I am having two arrays, Namely
我有两个阵列,即
NMutableArray* first;
NMutableArray* second;
Now I am copying first object to the second array like
现在我将第一个对象复制到第二个数组中
for (int i=0;i<first.count; i++)
{
[second addObject:[first objectAtIndex:i];
}
This is ok. I don't know how to access the value of the First Array. I tried like this ,
还行吧。我不知道如何访问First Array的值。我试过这样的,
[second addObject:[[first objectAtIndex:i]name]];
I want to get the name value which is in the first object of first array. I tried using the above line, it is showing some warning. Please help me
我想获取第一个数组的第一个对象中的名称值。我尝试使用上面的一行,它显示了一些警告。请帮帮我
3 个解决方案
#1
7
Assuming you started with an array like this:
假设你开始使用这样的数组:
NSArray *array1 = @[@{@name : @"Fred"},
@{@name : @"Bill"}];
You could create a second array that contains the value of a given property of each element of the first array as follows:
您可以创建第二个数组,其中包含第一个数组的每个元素的给定属性的值,如下所示:
NSArray *array2 = [array1 valueForKey:@"name"];
If you then logged the second array...
如果你然后记录了第二个数组......
NSLog(@"%@", array2);
...the resulting output would be
......产生的结果将是
2012-04-18 16:26:11.226 ExampleRunner[23320:707] (
Fred,
Bill
)
EDIT
Note that this will work regardless of whether the objects in the first array are instances of NSDictionary
as shown in the example above, or instances of a class or classes that have a name
property or instance variable (or an _name
instance variable, for that matter). For more information on how and why this works, see the documentation for the NSKeyValueCoding
informal protocol:
请注意,无论第一个数组中的对象是否是上面示例中所示的NSDictionary实例,或者具有name属性或实例变量的类或类的实例(或者_name实例变量),这都将起作用)。有关其工作原理和原因的更多信息,请参阅NSKeyValueCoding非正式协议的文档:
#2
3
The brackets are currently in the wrong place:
括号目前位于错误的位置:
[second addObject:[[first objectAtIndex:i] name]];
#3
1
Updated Answer:
Again, I think you should split stuff out into easy to parse lines of code:
同样,我认为你应该把东西分成易于解析的代码行:
for (id theObject in first)
{
// without an actual type, I still think the compiler might
// throw a warning on this next line of code;
// but maybe RJR III is correct and it won't warn.
// I didn't check.
NSString * nameOfObject = [theObject name];
if(nameOfObject)
{
[second addObject:nameOfObject];
}
}
Notice that I do some error checking in here as well (i.e. making sure the name is not nil).
请注意,我在这里也做了一些错误检查(即确保名称不是nil)。
Original Answer:
You're getting a warning because the compiler doesn't know what kind of custom object is being fetched from your call to "[first objectAtIndex: i]
". In other words, it doesn't know what kind of object you're trying to get the "name
" of.
您收到警告是因为编译器不知道从您对“[first objectAtIndex:i]”的调用中获取了哪种自定义对象。换句话说,它不知道你想要获得什么样的对象的“名称”。
Cast it to the right type and you'll get rid of the warning.
将它转换为正确的类型,你将摆脱警告。
Or even better, split that one line of multiple things happening at once into two or three lines of code and make your code more readable in the process.
或者甚至更好,将一行多行事件分成两行或三行代码,并使代码在此过程中更具可读性。
#1
7
Assuming you started with an array like this:
假设你开始使用这样的数组:
NSArray *array1 = @[@{@name : @"Fred"},
@{@name : @"Bill"}];
You could create a second array that contains the value of a given property of each element of the first array as follows:
您可以创建第二个数组,其中包含第一个数组的每个元素的给定属性的值,如下所示:
NSArray *array2 = [array1 valueForKey:@"name"];
If you then logged the second array...
如果你然后记录了第二个数组......
NSLog(@"%@", array2);
...the resulting output would be
......产生的结果将是
2012-04-18 16:26:11.226 ExampleRunner[23320:707] (
Fred,
Bill
)
EDIT
Note that this will work regardless of whether the objects in the first array are instances of NSDictionary
as shown in the example above, or instances of a class or classes that have a name
property or instance variable (or an _name
instance variable, for that matter). For more information on how and why this works, see the documentation for the NSKeyValueCoding
informal protocol:
请注意,无论第一个数组中的对象是否是上面示例中所示的NSDictionary实例,或者具有name属性或实例变量的类或类的实例(或者_name实例变量),这都将起作用)。有关其工作原理和原因的更多信息,请参阅NSKeyValueCoding非正式协议的文档:
#2
3
The brackets are currently in the wrong place:
括号目前位于错误的位置:
[second addObject:[[first objectAtIndex:i] name]];
#3
1
Updated Answer:
Again, I think you should split stuff out into easy to parse lines of code:
同样,我认为你应该把东西分成易于解析的代码行:
for (id theObject in first)
{
// without an actual type, I still think the compiler might
// throw a warning on this next line of code;
// but maybe RJR III is correct and it won't warn.
// I didn't check.
NSString * nameOfObject = [theObject name];
if(nameOfObject)
{
[second addObject:nameOfObject];
}
}
Notice that I do some error checking in here as well (i.e. making sure the name is not nil).
请注意,我在这里也做了一些错误检查(即确保名称不是nil)。
Original Answer:
You're getting a warning because the compiler doesn't know what kind of custom object is being fetched from your call to "[first objectAtIndex: i]
". In other words, it doesn't know what kind of object you're trying to get the "name
" of.
您收到警告是因为编译器不知道从您对“[first objectAtIndex:i]”的调用中获取了哪种自定义对象。换句话说,它不知道你想要获得什么样的对象的“名称”。
Cast it to the right type and you'll get rid of the warning.
将它转换为正确的类型,你将摆脱警告。
Or even better, split that one line of multiple things happening at once into two or three lines of code and make your code more readable in the process.
或者甚至更好,将一行多行事件分成两行或三行代码,并使代码在此过程中更具可读性。