使用 Swift 而不是 Objective-C 从解析数据中保存对象
Saving Objects from Parse data using Swift instead of Objective-C
我正在将我的 ViewController
从 Objective-C 转换为 Swift。
我想知道 findObjectsInBackgroundWithBlock
是否仍然使用 是最明智的选择(或者加载速度是否太慢),以及我是否应该使用它同理。
我正在使用它来获取我的 Parse
数据并将其保存到我的 App Group
and/or NSUserDefaults
.
// Query Parse
PFQuery *query = [PFQuery queryWithClassName:@"data"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *localMatchup = [@[] mutableCopy];
for (PFObject *object in objects) {
// Add objects to local Arrays
[localMatchup addObject:[object objectForKey:@"matchup"]];
// App Group
NSString *container = @"group.com.ramsden.playoffs";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// Matchup
[defaults setObject:localMatchup forKey:@"KeyMatchup"];
NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
self.matchupArray = localMatchup;
更新:
我在下面尝试了 Lamar 的回答,但在 findObjectsInBackgroundWithBlock 行中收到错误消息:'([AnyObject]!, NSError!) -> Void' is not compatible to 'PFArrayResultBlock?
下面仔细看看具体的行:
试试这个:
func GetBackFromParse(localMatchUp:NSMutableArray){
var query = PFQuery(className: "data")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objects = objects as? [PFObject]{
for SingleObject in objects
{
var matchup = SingleObject["matchup"]
localMatchUp.addObject(matchup!)
// do whatever you want to do
}
}
}
}
我正在将我的 ViewController
从 Objective-C 转换为 Swift。
我想知道 findObjectsInBackgroundWithBlock
是否仍然使用 是最明智的选择(或者加载速度是否太慢),以及我是否应该使用它同理。
我正在使用它来获取我的 Parse
数据并将其保存到我的 App Group
and/or NSUserDefaults
.
// Query Parse
PFQuery *query = [PFQuery queryWithClassName:@"data"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *localMatchup = [@[] mutableCopy];
for (PFObject *object in objects) {
// Add objects to local Arrays
[localMatchup addObject:[object objectForKey:@"matchup"]];
// App Group
NSString *container = @"group.com.ramsden.playoffs";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// Matchup
[defaults setObject:localMatchup forKey:@"KeyMatchup"];
NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
self.matchupArray = localMatchup;
更新:
我在下面尝试了 Lamar 的回答,但在 findObjectsInBackgroundWithBlock 行中收到错误消息:'([AnyObject]!, NSError!) -> Void' is not compatible to 'PFArrayResultBlock?
试试这个:
func GetBackFromParse(localMatchUp:NSMutableArray){
var query = PFQuery(className: "data")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let objects = objects as? [PFObject]{
for SingleObject in objects
{
var matchup = SingleObject["matchup"]
localMatchUp.addObject(matchup!)
// do whatever you want to do
}
}
}
}