[英]How to add one NSArray data to another NSArray fields?
I tought question title is little bit mismatch , can you please see the following explanation... 我题字标题有点不匹配,请看下面的解释...
Currently i am having 2 NSArray's of data: 目前我有2个NSArray的数据:
NSArray *arr1 = [[NSArray alloc]initWithObjects:@"aa",@"bb",@"1",@"cc", nil];
NSArray *arr2 = @[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
In 1st Array i got data from server 在第一阵列中,我从服务器获取数据
I need to load those parameters in specific UILabel's in 2nd Array 我需要将这些参数加载到第二数组的特定UILabel中
I am looking O/P is :: 我在看O / P是 ::
self.lbl1.text = @"aa";
self.lbl2.text = @"bb";
self.lbl3.text = @"1";
self.lbl4.text = @"cc";
Is there any possiblity, can you please help me out.. 有什么可能的,请你帮帮我。
in ViewController.h 在ViewController.h中
@property (strong, nonatomic)IBOutlet UILabel* lbl1;
@property (strong, nonatomic)IBOutlet UILabel* lbl2;
@property (strong, nonatomic)IBOutlet UILabel* lbl3;
@property (strong, nonatomic)IBOutlet UILabel* lbl4;
in ViewController.m 在ViewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *arr1 = [[NSArray alloc]initWithObjects:@"aa",@"bb",@"1",@"cc", nil];
NSArray *arr2 = @[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
for(int i=0;(i<[arr1 count])&&(i<[arr2 count]);i++)
{
UILabel *label = (UILabel*)[arr2 objectAtIndex:i];
label.text = (NSString*)[arr1 objectAtIndex:i];
}
NSLog(@"%@,\n %@,\n %@, \n%@",self.lbl1,self.lbl2,self.lbl3,self.lbl4);
} }
CRASH like: 崩溃像:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
You can't directly do that but you can set arr2
to: 您不能直接这样做,但可以将arr2
设置为:
arr2 = @[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
and then 接着
for (UILabel *lbl in arr2) {
NSInteger i=[arr2 indexOfObject:lbl];
lbl.text = [arr1 objectAtIndex:i];
}
Store the labels in the arrays, not their text property. 将标签存储在数组中,而不是文本属性中。
NSArray *arr1 = [[NSArray alloc]initWithObjects:@"aa",@"bb",@"1",@"cc", nil];
NSArray *arr2 = @[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
for(int i=0;(i<[arr1 count])&&(i<[arr2 count]);i++)
{
UILabel *label = (UILabel*)[arr2 objectAtIndex:i];
label.text = (NSString*)[arr1 objectAtIndex:i];
}
Checking for count of both like : (i<[arr1 count])&&(i<[arr2 count])
ensures that the app does not crash if somehow the arrays become of different count. 检查两个计数: (i<[arr1 count])&&(i<[arr2 count])
可确保如果数组的计数不同,应用程序也不会崩溃。
Assuming both arrays have the same size, you can do: 假设两个数组的大小相同,则可以执行以下操作:
NSArray *arr3 = @[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
for (NSUInteger i = 0; i < arr1.count; i++) {
arr3[i].text = arr1[i];
}
If the size of the array received from the server may vary, you can use MIN(arr1.count, arr3.count)
, ie minimum of the two sizes, so you won't get an out-of-bounds exception: 如果从服务器收到的数组大小可能有所不同,则可以使用MIN(arr1.count, arr3.count)
,即两个大小中的最小值,因此不会出现越界异常:
for (NSUInteger i = 0; i < MIN(arr1.count, arr3.count); i++) {
arr3[i].text = arr1[i];
}
Short answer: It is Not possible the way you want to do it. 简短的答案:这是不可能的。
This is because self.lbl1.text
is a NSString and not attached to self.lbl1
anymore ( text
is a copy property of UILabel). 这是因为self.lbl1.text
是一个NSString,并且不再附加到self.lbl1
( text
是UILabel的copy属性)。 If you change this NSString, you would not change the value of self.lbl1.text
如果更改此NSString,则不会更改self.lbl1.text
的值
A better approach may be to save only the label, not its value: 更好的方法可能是仅保存标签,而不保存其值:
NSArray *arr2 = @[self.lbl1, self.lbl2, self.lbl3, self.lbl4];
In this case, you could loop through your array: 在这种情况下,您可以遍历数组:
for (NSInteger i=0; i < arr2.count && i < arr1.count; i++) {
((UILabel*)[arr2 objectAtIndex:i]).text = [arr1 objectAtIndex:i];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.