Swift MagicalRecord saveWithBlock 语法
Swift syntax for MagicalRecord saveWithBlock
在objC中saveWithBlock的方式是
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
// This block runs in background thread
} completion:^(BOOL success, NSError *error) {
// This block runs in main thread
}];
我这辈子都无法获得正确的 Swift block/closure 语法。不知道为什么这不适合我。有人可以帮忙制作上面的 swift 版本吗?
它应该看起来像这样:在我的脑海中...
MagicalRecord.saveWithBlock({ (localContext : NSManagedObjectContext!) in
// This block runs in background thread
}, completion: { (success : Bool, error : NSError!) in
// This block runs in main thread
})
对于 Swift 3.0 和 MagicalRecord 2.3.2
MagicalRecord.saveWithBlock({
(localContext: NSManagedObjectContext!) in
// Do your work to be saved here, against the `localContext` instance
// Everything you do in this block will occur on a background thread
}, completion: {
(MRSaveCompletionHandler) in
// This block runs in main thread
})
试试这个
MagicalRecord.save({ (localContext:NSManagedObjectContext) in
}, completion:{ (success: Bool,error: NSError!) in
} as? MRSaveCompletionHandler
)
下面给出了 Swift4 语法,试试这个
MagicalRecord.save({ (localContext) in
// This block runs in background thread
}) { (success, error) in
// This block runs in main thread
}
希望对您有所帮助。
在objC中saveWithBlock的方式是
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
// This block runs in background thread
} completion:^(BOOL success, NSError *error) {
// This block runs in main thread
}];
我这辈子都无法获得正确的 Swift block/closure 语法。不知道为什么这不适合我。有人可以帮忙制作上面的 swift 版本吗?
它应该看起来像这样:在我的脑海中...
MagicalRecord.saveWithBlock({ (localContext : NSManagedObjectContext!) in
// This block runs in background thread
}, completion: { (success : Bool, error : NSError!) in
// This block runs in main thread
})
对于 Swift 3.0 和 MagicalRecord 2.3.2
MagicalRecord.saveWithBlock({
(localContext: NSManagedObjectContext!) in
// Do your work to be saved here, against the `localContext` instance
// Everything you do in this block will occur on a background thread
}, completion: {
(MRSaveCompletionHandler) in
// This block runs in main thread
})
试试这个
MagicalRecord.save({ (localContext:NSManagedObjectContext) in
}, completion:{ (success: Bool,error: NSError!) in
} as? MRSaveCompletionHandler
)
下面给出了 Swift4 语法,试试这个
MagicalRecord.save({ (localContext) in
// This block runs in background thread
}) { (success, error) in
// This block runs in main thread
}
希望对您有所帮助。