1
CoreData値を後でテーブルビューで表示できるように、配列に保存しようとしています。しかし、これまでのところ、ログはアプリケーションにデータがあるのに配列が空(null)であることを示しています。なぜこのようなことが起こるのか分かりません。誰かが私に助けを与えることができますか?IOS/Objective-C:コアデータ値をNSArrayに保存できません
@interface TableViewController()
@property (strong) NSMutableArray *locations;
@end
@implementation TableViewController
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate respondsToSelector:@selector(managedObjectContext)]){
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
NSError *error;
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Spot"];
self.locations = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
[self.tableView reloadData];
NSLog(@"%@ Places are: ", _locations);
}
EDIT:AppDelegate.m:
あなたはコアデータ・スタックを作成するのです+ (AppDelegate *)sharedDelegate {
return (AppDelegate*)[UIApplication sharedApplication].delegate;
}
- (NSURLSession *)getURLSession
{
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
NSURLSessionConfiguration *configuration =[NSURLSessionConfiguration defaultSessionConfiguration];
session = [NSURLSession sessionWithConfiguration:configuration];
});
return session;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https:franciscocosta.net/lisbon-spots"]];
NSURLSessionDataTask *task = [[self getURLSession] dataTaskWithRequest:request completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error){
dispatch_async(dispatch_get_main_queue(),^{
NSError *error;
NSArray *mainLocations = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
for(NSDictionary *dict in mainLocations) {
[Spot spotWithDict:dict];
}
});
}];
[task resume];
return YES;
}
のスタックの作成に見てみてもいいですか?なぜ、「場所」は不変だと宣言されていますが、変更可能なものが割り当てられていますか? – vadian
これは編集されましたが、ログは空のままです... – FuManchu
あなたはまだエラーを無視しています。それには何も含まれていますか? – vadian