gpt4 book ai didi

ios - [__NSCFDictionary enumerateObjectsUsingBlock :]: unrecognized selector sent to instance 0x101918a10'

转载 作者:行者123 更新时间:2023-12-01 19:14:59 25 4
gpt4 key购买 nike

我正在从我的 json 文件中读取 jason 数据,并且我成功打印了 NSarray 类型 arraytsik,现在当我尝试使用 enumerateObjectsUsingBlock 循环遍历 arraytsik 时,它给了我输出或数组错误

 2012-12-17 16:59:04.711 tsikzoeMig[6416:303] here i am printing the array {
tsik = (
{
author = 2;
authorName = fgfg;
dateSubmitted = "2012-08-19 00:00:00";
dictionaryType = 1;
id = 1;
word = fgdf;
wordDef = fdgdfg;
},
{
author = 2;
authorName = "\U0f51\U0f74\U0f44\U0f0b\U0f51\U0f40\U0f62\U0f0b\U0f5a\U0f72\U0f42\U0f0b\U0f58\U0f5b\U0f7c\U0f51\U0f0d";
dateSubmitted = "2012-08-19 00:00:00";
dictionaryType = 1;
id = 2;
word = "\U0f21\U0f20\U0f24\U0f20\U0f56\U0f66\U0f7c\U0f0d";
wordDef = "\U0f56\U0f66\U0f7c\U0f0b\U0f56\U0f66\U0f7c\U0f0b\U0f5e\U0f7a\U0f66\U0f0b\U0f54\U0f60\U0f72\U0f0b\U0f66\U0f92\U0fb2\U0f60\U0f72\U0f0b\U0f41\U0fb1\U0f51\U0f0b\U0f54\U0f62\U0f0b\U0f42\U0fb1\U0f72\U0f0b\U0f58\U0f72\U0f44\U0f0c\U0f0d";
}
);
}
2012-12-17 16:59:04.712 tsikzoeMig[6416:303] -[__NSCFDictionary enumerateObjectsUsingBlock:]: unrecognized selector sent to instance

0x102114700 2012-12-17 16:59:04.713 tsikzoeMig[6416:303] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary enumerateObjectsUsingBlock:]: unrecognized selector sent to instance 0x102114700' * First throw call stack: ( 0 CoreFoundation 0x00007fff8a1190a6 exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff8a2963f0 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8a1af6ea -[NSObject(NSObject) doesNotRecognizeSelector:] + 186 3 CoreFoundation 0x00007fff8a1075ce __forwarding + 414 4 CoreFoundation 0x00007fff8a1073b8 _CF_forwarding_prep_0 + 232 5 tsikzoeMig 0x0000000100001cd3 main + 851 6 libdyld.dylib 0x00007fff8acd97e1 start + 0 7 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminate called throwing an exception (lldb)



我的 main.m 代码在这里
//  main.m

#import "TsikzoeE.h"
static NSManagedObjectModel *managedObjectModel()
{
static NSManagedObjectModel *model = nil;
if (model != nil) {
return model;
}

NSString *path = @"tsikzoe";
NSURL *modelURL = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"momd"]];
model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

return model;
}

static NSManagedObjectContext *managedObjectContext()
{
static NSManagedObjectContext *context = nil;
if (context != nil) {
return context;
}

@autoreleasepool {
context = [[NSManagedObjectContext alloc] init];

NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel()];
[context setPersistentStoreCoordinator:coordinator];

NSString *STORE_TYPE = NSSQLiteStoreType;

NSString *path = [[NSProcessInfo processInfo] arguments][0];
path = [path stringByDeletingPathExtension];
NSURL *url = [NSURL fileURLWithPath:[path stringByAppendingPathExtension:@"sqlite"]];

NSError *error;
NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPE configuration:nil URL:url options:nil error:&error];

if (newStore == nil) {
NSLog(@"Store Configuration Failure %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
}
}
return context;
}

int main(int argc, const char * argv[])
{

@autoreleasepool {
// Create the managed object context
NSManagedObjectContext *context = managedObjectContext();

// Custom code here...
// Save the managed object context
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Error while saving %@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
NSError* err = nil;

NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"tsikzoe" ofType:@"json"];
NSArray* arraytsik = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]options:kNilOptions error:&err];

// array outputing here to check

NSLog(@"here i am printing the array %@",arraytsik);


[arraytsik enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {


TsikzoeE *tsikz = [NSEntityDescription insertNewObjectForEntityForName:@"TsikzoeE"inManagedObjectContext:context];
tsikz.id = [obj objectForKey:@"id"];
tsikz.dictionaryType=[obj objectForKey:@"dictionaryType"];
tsikz.word=[obj objectAtIndex:3];
tsikz.wordDef=[obj objectForKey:@"wordDef"];
tsikz.author=[obj objectForKey:@"author"];
tsikz.authorName=[obj objectForKey:@"authorName"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
}];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"TsikzoeE"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (TsikzoeE *tsikz in fetchedObjects) {
NSLog(@"word: %@", tsikz.word);

NSLog(@"wordDef: %@", tsikz.wordDef);
}

}
return 0;
}

我的 json 内容是
{"tsik":[{"id":1,"dictionaryType":1,"word":"fgdf","wordDef":"fdgdfg","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"fgfg"},{"id":2,"dictionaryType":1,"word":"༡༠༤༠བསོ།","wordDef":"བསོ་བསོ་ཞེས་པའི་སྒྲའི་ཁྱད་པར་གྱི་མིང༌།","dateSubmitted":"2012-08-19 00:00:00","author":2,"authorName":"དུང་དཀར་ཚིག་མཛོད།"}]}

我的 TsikzoeE.h 代码
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface TsikzoeE : NSManagedObject

@property (nonatomic, retain) NSNumber * author;
@property (nonatomic, retain) NSString * authorName;
@property (nonatomic, retain) NSNumber * dictionaryType;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * word;
@property (nonatomic, retain) NSString * wordDef;


@end

tsikzoeE.m 代码
  //
// TsikzoeE.m


#import "TsikzoeE.h"


@implementation TsikzoeE

@dynamic author;
@dynamic authorName;
@dynamic dictionaryType;
@dynamic id;
@dynamic word;
@dynamic wordDef;

@end

最佳答案

JSON 数据包含一个字典(带有一个键“tsik”)而不是一个数组。

以下应该更好地工作:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
NSArray *arraytsik = [dict objectForKey:@"tsik"];
enumerateObjectsUsingBlock 内部似乎也有问题 block : obj这里是字典,所以
tsikz.word=[obj objectAtIndex:3];

不起作用。也许你的意思是
tsikz.word=[obj objectForKey:@"word"];

备注:您还可以考虑在循环完成后保存托管对象上下文,而不是为添加的每个项目保存它。

关于ios - [__NSCFDictionary enumerateObjectsUsingBlock :]: unrecognized selector sent to instance 0x101918a10',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13913499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com