我是iOS开发的新手我遇到了一个标签不想在NSArray中显示值的问题。请有人帮忙指出哪里出错了,为什么?
// given rows and cols as the number of rows and columns of the matrices
size_t max_index = rows * cols - 1;
for ( size_t i = 0, j = 0; i <= max_index; ++i ) {
c_rm[i] = a_rm[i] + b_cm[j];
j += rows;
if ( j > max_index )
j -= max_index;
}
运行代码时得到的结果:NSLog打印正确的值,但setText方法打印“(”到标签。
这是代码,因为我没有放入URL,运行代码时没有错误。
projectArray在.h文件中定义为:@property(nonatomic,copy)NSArray * projectArray;
这是在NSLog中打印的内容:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
json_data = [json objectForKey:@"data"];
content = [json_data objectForKey:@"content"];
NSMutableArray *contentTemp = [[NSMutableArray alloc] initWithCapacity:content.count];
for (NSDictionary *contentInfo in content) {
project_details *mvm = [[project_details alloc] init];
mvm.bname = contentInfo[@"bname"];
mvm.burl = contentInfo[@"burl"];
mvm.p_num = contentInfo[@"p_id"];
mvm.pname = contentInfo[@"pname"];
mvm.pdesc = contentInfo[@"pdesc"];
mvm.purl = contentInfo[@"purl"];
mvm.plive = contentInfo[@"plive"];
mvm.pcompleted = contentInfo[@"pcompleted"];
mvm.days = contentInfo[@"days"];
mvm.pfunds = contentInfo[@"pfunds"];
mvm.amount = contentInfo[@"amount"];
mvm.perc = contentInfo[@"perc"];
[contentTemp addObject:mvm];
}
self.projectArray = contentTemp;
dispatch_async(dispatch_get_main_queue(), ^{
[self.label setText:[NSString stringWithFormat:@"%@", [self.projectArray valueForKey:@"bname"]]];
NSLog(@"%@", [self.projectArray valueForKey:@"bname"]);
});
}];
[dataTaskProject resume];
答案 0 :(得分:0)
在NSArray上调用valueForKey
会返回NSArray。你可能想要的是[(mvm *)self.projectArray.firstObject bname]
。您的self.projectArray
包含一个或多个mvm
个对象,这些对象具有bname
属性。
您可以将.firstObject
更改为数组索引(即[1]
,[2]
等),具体取决于您要在阵列中访问的内容(它不清楚)来自你的代码给我。