ios 使用 NSArray 初始化对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14970412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize Array of Objects using NSArray
提问by Howdy_McGee
I'm pretty new to Objective-C and iOS so I've been playing around with the Picker View. I've defined a Person Class so that when you create a new Person it automatically gives that person a name and age.
我对 Objective-C 和 iOS 还是很陌生,所以我一直在玩 Picker View。我已经定义了一个 Person 类,这样当你创建一个新的 Person 时,它会自动给那个人一个名字和年龄。
#import "Person.h"
@implementation Person
@synthesize personName, age;
-(id)init
{
self = [super init];
if(self)
{
personName = [self randomName];
age = [self randomAge];
}
return self;
}
-(NSString *) randomName
{
NSString* name;
NSArray* nameArr = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin", @"Meg Griffin", @"Hyman Lolwut",
@"Mike Roflcoptor", @"Cindy Woods", @"Jessica Windmill", @"Alexander The Great",
@"Sarah Peterson", @"Scott Scottland", @"Geoff Fanta", @"Amanda Pope", @"Michael Meyers",
@"Richard Biggus", @"Montey Python", @"Mike Wut", @"Fake Person", @"Chair",
nil];
NSUInteger randomIndex = arc4random() % [nameArr count];
name = [nameArr objectAtIndex: randomIndex];
return name;
}
-(NSInteger *) randomAge
{
//lowerBound + arc4random() % (upperBound - lowerBound);
NSInteger* num = (NSInteger*)(1 + arc4random() % (99 - 1));
return num;
}
@end
Now I want to make an array of Persons so I can throw a bunch into the picker, pick one Person and show their age. First though I need to make an array of Persons. How do I make an array of objects, initialize and allocate them?
现在我想制作一个 Person 数组,这样我就可以将一堆扔到选择器中,选择一个 Person 并显示他们的年龄。首先,虽然我需要制作一系列人员。如何创建对象数组、初始化和分配它们?
回答by Allen Zeng
There is also a shorthand of doing this:
这样做还有一个简写:
NSArray *persons = @[person1, person2, person3];
It's equivalent to
它相当于
NSArray *persons = [NSArray arrayWithObjects:person1, person2, person3, nil];
As iiFreeman said, you still need to do proper memory management if you're not using ARC.
正如 iiFreeman 所说,如果您不使用 ARC,您仍然需要进行适当的内存管理。
回答by iiFreeman
NSMutableArray *persons = [NSMutableArray array];
for (int i = 0; i < myPersonsCount; i++) {
[persons addObject:[[Person alloc] init]];
}
NSArray *arrayOfPersons = [NSArray arrayWithArray:persons]; // if you want immutable array
also you can reach this without using NSMutableArray:
您也可以在不使用 NSMutableArray 的情况下达到此目的:
NSArray *persons = [NSArray array];
for (int i = 0; i < myPersonsCount; i++) {
persons = [persons arrayByAddingObject:[[Person alloc] init]];
}
One more thing - it's valid for ARC enabled environment, if you going to use it without ARC don't forget to add autoreleased objects into array!
还有一件事 - 它对启用 ARC 的环境有效,如果您要在没有 ARC 的情况下使用它,请不要忘记将自动释放的对象添加到数组中!
[persons addObject:[[[Person alloc] init] autorelease];
回答by gnasher729
No one commenting on the randomAge
method?
没有人评论该randomAge
方法吗?
This is so awfully wrong, it couldn't be any wronger.
这是非常错误的,它不能有任何错误。
NSInteger
is a primitive type - it is most likely typedef'd as int
or long
.
In the randomAge
method, you calculate a number from about 1 to 98.
NSInteger
是一种原始类型 - 它很可能被 typedef 为int
or long
。在该randomAge
方法中,您计算一个从 1 到 98 之间的数字。
Then you can cast that number to an NSNumber. You had to add a cast because the compiler gave you a warning that you didn't understand. That made the warning go away, but left you with an awful bug: That number was forced to be a pointer, so now you have a pointer to an integer somewhere in the first 100 bytes of memory.
然后您可以将该数字转换为NSNumber。您必须添加强制转换,因为编译器给了您一个您不理解的警告。这让警告消失了,但给你留下了一个可怕的错误:那个数字被迫成为一个指针,所以现在你有一个指向内存前 100 字节某处的整数的指针。
If you access an NSInteger
through the pointer, your program will crash. If you write through the pointer, your program will crash. If you put it into an array or dictionary, your program will crash.
如果您NSInteger
通过指针访问 an ,您的程序将崩溃。如果您通过指针写入,您的程序将崩溃。如果你把它放入数组或字典中,你的程序就会崩溃。
Change it either to NSInteger
or int
, which is probably the best, or to NSNumber
if you need an object for some reason. Then create the object by calling [NSNumber numberWithInteger:99]or whatever number you want.
将其更改为NSInteger
或int
,这可能是最好的,或者NSNumber
如果您出于某种原因需要一个对象。然后通过调用[NSNumber numberWithInteger:99]或任何你想要的数字来创建对象。
回答by JaKK's Team
This might not really answer the question, but just in case someone just need to quickly send a string valueto a function that require a NSArray parameter.
这可能不会真正回答问题,但以防万一有人只需要快速将字符串值发送到需要 NSArray 参数的函数。
NSArray *data = @[@"The String Value"];
if you need to send more than just 1 string value, you could also use
如果您需要发送的不仅仅是 1 个字符串值,您也可以使用
NSArray *data = @[@"The String Value", @"Second String", @"Third etc"];
then you can send it to the function like below
然后您可以将其发送到如下所示的功能
theFunction(data);
回答by Mr. Pravin Kantariya
This way you can Create NSArray, NSMutableArray.
这样你就可以创建 NSArray、NSMutableArray。
NSArray keys =[NSArray arrayWithObjects:@"key1",@"key2",@"key3",nil];
NSArray objects =[NSArray arrayWithObjects:@"value1",@"value2",@"value3",nil];