Date: 04/2020

cocoapod:

删除cocoapod
rm -rf "${HOME}/Library/Caches/CocoaPods"
rm -rf "`pwd`/Pods/"
pod update
pod for RealmSwift
use_frameworks!
target 'Agents Partner' do
pod 'RealmSwift'
end

xcode/xVim2

sudo codesign -f -s XcodeSigner /Applications/Xcode.app
cd XVim2; make

Other

protocol MKAnnotation: 这个是地图上的标记接口,包括两个标题和坐标位置
MKMapView
unwind segue这个貌似我之前都没有听说过,可能基本上不用Segue的原因,做起来很简单。1)在目标VC代码里面unwind action,2)在源VC的xib里面添加exit的unwind segue,3)还可以绑定退出事件
https://developer.apple.com/documentation/uikit/resource_management/dismissing_a_view_controller_with_an_unwind_segue
A .dSYM file is a debug symbols file.将json文件转成NSDictionary
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? NSDictionary
将json直接转成Codable,无需Dictionary
let content = try JSONDecoder().decode(Content.self, from: data)
Realm accessed from incorrect thread -这个错误在与realm对象不是线程安全的,这个错误很常见。使用dataTask作为一般简单http请求,对于这个也可以采用downloadTask
URLSession.shared.dataTask(with: request) { [weak self] (data, response, error) in }
JSON文件格式有两种,一种是和XML对应来的,有root,但一般而言也可以没有root,如:
{"v": 1, "d": "dd"}

04/07

String format:
String(format: "Value: %3.2f\tResult: %3.2f", arguments: [2.7, 99.8])
let message = "You've earned \(ptsString) points"
main queue 也有sync和async,貌似不能在sync里面再call sync,
DispatchQueue.main.sync {[weak self] in }
最近在realm里面经常看到这样调用,加label:A string label to attach to the queue to uniquely identify it in debugging tools
DispatchQueue(label: "background").async { }
try/catch:
func canThrowErrors() throws -> String //
let x = try? someMethod() // Void函数也可以这样
let x = try! someMethod() // 也可以省略 let x,
do {
try someMethod()
}catch is SomeException{
}
do {
let realm = try Realm()
} catch let error as NSError {
// handle error
}
Realm: https://realm.io/docs/swift/latest/#queries
let realm = try! Realm()
let config = realm.configuration.fileURL
let folder = realm.configuration.fileURL!.deletingLastPathComponent().path
//add new record
try! realm.write { realm.add(myDog) }
//可以设置多个config文件,也就是多个db
Realm.Configuration.defaultConfiguration = config
// 内存db
let realm = try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "MyInMemoryRealm"))
Realm的文件以及辅助文件
.realm
.realm.lock
.realm.management
.realm.note
Realm supports the following property types: Bool, Int, Int8, Int16, Int32, Int64, Double, Float, String, Date, and Data.class Person: Object {
@objc dynamic var id = 0
@objc dynamic var name = ""

override static func primaryKey() -> String? {
return "id"
}
override static func indexedProperties() -> [String] {
return ["name"]
}
}
Like primary keys, indexes make writes slightly slower, but makes queries using equality and IN operators faster. 主健和index都会导致写入变慢
let myPuppy = realm.objects(Dog.self) // 获取Dog全部对象
let myPuppy = realm.objects(Dog.self).filter("age == 1").first
let myPuppy = realm.objects(Dog.self).filter("age == 1").first
try! realm.write {
myPuppy!.age = 2 //采用这种方式update数据而无需主键
}
采用类似json数据来初始化:
let duck = Duck(value: [ "animal": [ "age": 3 ], "name": "Gustav" ])
这也是一种方式
let persons = realm.objects(Person.self)
try! realm.write {
persons.first?.setValue(true, forKey: "isFirst")
// set each person's planet property to "Earth"
persons.setValue("Earth", forKey: "planet")
}
// Assuming a "Book" with a primary key of `1` already exists.
try! realm.write {
realm.create(Book.self, value: ["id": 1, "price": 9000.0], update: .modified)
// the book's `title` property will remain unchanged.
}
配合predicate使用搜索疑问:看上去并没有查询?如把全部数据都load到内存中,这也有点不那么友好