swift和OC 项目使用
1 #pragma mark 修改 // MARK:
2 计算高度
extension String {
func ga_heightForComment(fontSize:CGFloat, width:CGFloat, maxHeight:CGFloat) -> CGFloat {
let font = UIFont.systemFont(ofSize: fontSize)
let rect = NSString(string:self).boundingRect(with: CGSize(width:width, height:maxHeight), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
// return rect.height
return ceil(rect.height)
}
}
swift 语法
1 as as! as?
// as
// 从派生类转换为基类 向上转型
let cat = Cat()
let animal = cat as Animal
// 消除二异 数值类型转换
let num1 = 42 as CGFloat
let num2 = 42 as Int
let num4 = (42 / 2) as Double
print("\(num4)")
// 类型判断
switch animal {
case let cat as Cat:
print("cat")
case let dog as Dog:
print("dog")
default:
break
}
// as! 强制转换 向下转型
// let asAnmail: Animal = Animal()
let asAnmail: Animal = Cat()
asAnmail.name = "cat"
print("\(asAnmail.name)")
let catLog = asAnmail as! Cat
// as? 。但 as? 如果转换不成功的时候便会返回一个 nil 对象。成功的话返回可选类型值(optional),需要我们拆包使用
let asasAnmail : Animal = Cat()
if let cat = animal as? Cat {
print("cat is not nil")
} else {
print("cat is nil")
}
2 扩展
// 结构体
struct RunTimeKey {
static let jkKey = UnsafeRawPointer.init(bitPattern: "JKKey".hashValue)
}
// 扩展属性
var jkPro:String? {
set {
objc_setAssociatedObject(self, ExtionViewController.RunTimeKey.jkKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
get {
return objc_getAssociatedObject(self, ExtionViewController.RunTimeKey.jkKey) as? String
}
}
3 UnsafePointer 指针结构体
4 4种传值
github.com/LiuDao27/pu…
5 计算
func ga_heightForComment(fontSize:CGFloat, width:CGFloat, maxHeight:CGFloat) -> CGFloat {
let font = UIFont.systemFont(ofSize: fontSize)
let rect = NSString(string:self).boundingRect(with: CGSize(width:width, height:maxHeight), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
// return rect.height
return ceil(rect.height)
}