我想获取 UILabel 中某些文本的长度,并动态更改 UILabel 的 width 属性。
但是我不知道如何在函数boundingrect中设置参数。这是 Apple Developer 的文档。
func boundingRect(with size: CGSize,
options: NSStringDrawingOptions = [],
context: NSStringDrawingContext?) -> CGRect
Run Code Online (Sandbox Code Playgroud)
,我试过这样
let attr = NSMutableAttributedString(string: "test test test test , this is test text. test test test test , this is test text. test test test test , this is test text. ")
//attr.addattributes
print(attr.boundingrect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), option: .truncatesLastVisibleLine
,context: nil)!)
Run Code Online (Sandbox Code Playgroud)
但最后我在打印时得到了错误的宽度,那么为什么以及如何获得正确的宽度。
NSArray.init(数组: [任意])
NSArray 有一个初始函数,其参数为 [Any]。但不是[Any?],那么,如何给这个数组添加一个nil值呢?
我的核心数据将再更新一个属性,为了避免崩溃,我首先添加了一个新的模型版本,此外:
关于这个问题的大部分关键是改变:
coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
Run Code Online (Sandbox Code Playgroud)
选项:在代码中为零
options:[NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption: true]
Run Code Online (Sandbox Code Playgroud)
但是在我的appdelegate.swift 中,我找不到任何“persistentStoreCoordinator”,那么我可以在我的版本中迁移 CoreData 吗?