ascii码转换 swift
2015-09-16 20:30:25 ltt_young 阅读数 4736
1、获取对象类型
OC:

NSDate* date = [NSDatedate];

NSLog(@"%@",NSStringFromClass([date class]));


Swift

letdate =NSDate()

letname = date.dynamicType


共有:

let name:AnyClass! = object_getClass(date)


2、函数入参中对象转化
OC转Swift,对象变为可选类型
Swift转OC,不用改变

3、枚举
1)Swift按位操作使用OptionSetType
struct MyOptions : OptionSetType {
    let rawValue: Int

    static let None         = MyOptions(rawValue: 0)
    static let FirstOption  = MyOptions(rawValue: 1 << 0)
    static let SecondOption = MyOptions(rawValue: 1 << 1)
    static let ThirdOption  = MyOptions(rawValue: 1 << 2)
}

Now we can use set-based semantics with MyOptions:

let singleOption = MyOptions.FirstOption
let multipleOptions: MyOptions = [.FirstOption, .SecondOption]
if multipleOptions.contains(.SecondOption) {
   
print("multipleOptions has SecondOption")
}
let allOptions = MyOptions(rawValue: 7)
if allOptions.contains(.ThirdOption) {
   
print("allOptions has ThirdOption")
}

2)OC调用Swift,只需增加前缀@objc
@objc enum Bear: Int {
    case Black, Grizzly, Polar
}

Shamelessly taken from the Swift Blog


In Objective-C this would look like

Bear type = BearBlack;
switch (type) {
   
case BearBlack:
   
case BearGrizzly:
   
case BearPolar:
       
[self runLikeHell];
}

3)Swift调用OC直接使用rawValue
2015-12-03 10:38:00 weixin_30408739 阅读数 5

http://www.ruanman.net/swift/learn/4741.html

 

转载于:https://www.cnblogs.com/lyggqm/p/5015355.html

2015-12-03 10:38:00 weixin_34323858 阅读数 15

http://www.ruanman.net/swift/learn/4741.html

 

2016-03-09 17:13:59 p495416980 阅读数 447
          //第一步: 创建一个 Scanner 的实例对象
            let scan = NSScanner(string: code!)
            //第二步: 定义一个可变的 UInt32 类型的变量用于接收
            var result: UInt32 = 0
            scan.scanHexInt(&result)
            //第三步: -> 转换成一个Unicode
            let unicode = UnicodeScalar(result)
            //第四步: 把 unicode转换成 Character
            let character = Character(unicode)
            
            emoji = "\(character)"
2018-05-02 18:08:54 u014305730 阅读数 177

import UIKit


extension UIColor {

    //用数值初始化颜色,便于生成设计图上标明的十六进制颜色

    convenience init(valueRGB: UInt, alpha: CGFloat) {

        self.init(

            red: CGFloat((valueRGB & 0xFF0000) >> 16) / 255.0,

            green: CGFloat((valueRGB & 0x00FF00) >> 8) / 255.0,

            blue: CGFloat(valueRGB & 0x0000FF) / 255.0,

            alpha: alpha

        )

    }

    convenience init(hexColor: String) {

        

        // 用十六进制颜色创建UIColor

        // 存储转换后的数值

        var red:UInt32 = 0, green:UInt32 = 0, blue:UInt32 = 0

        

        // 分别转换进行转换

        Scanner(string: hexColor[0..<2]).scanHexInt32(&red)


        Scanner(string: hexColor[2..<4]).scanHexInt32(&green)

        

        Scanner(string: hexColor[4..<6]).scanHexInt32(&blue)

        

        self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)

    }

}


extension String {

    

    // String使用下标截取字符串

    // 例: "示例字符串"[0..<2] 结果是 "示例"

    subscript (r: Range<Int>) -> String {

        get {

            let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)

            let endIndex = self.index(self.startIndex, offsetBy: r.upperBound)

            

            return self[startIndex..<endIndex]

        }

    }

}


没有更多推荐了,返回首页