xcode 如何将 NSArray 字符串对象值分配给其他 NSString 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9702361/
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
How to assign the NSArray string object values to other NSString variables
提问by Omer Waqas Khan
NSMutableArray *nameArray = [[NSMutableArray alloc] init];
[nameArray addObject:@"abc"];
[nameArray addObject:@"cdf"];
[nameArray addObject:@"jkl"];
//Use a for each loop to iterate through the array
for (NSString *s in nameArray) {
NSLog(@"value is %@", s);
}
The above code shows all the values of nameArray. But I want to assign all those values to these NSString:
上面的代码显示了 nameArray 的所有值。但我想将所有这些值分配给这些 NSString:
NSString *a;
NSString *b;
NSString *cd;
An aray can have 1 or more elements but not more than 5.
一个数组可以有 1 个或多个元素,但不能超过 5 个。
Actually,I have 5 buttons, each button on click will add a NSString value(values are: f1,f2,f3,f4 and f5) to NSMutableArray. Now its upto the user if he clicks 2 buttons or 3 or 5 in a day. Now all these values will be saved in NSMutableArray (which can be 1 or 2 but not more than 5). That NSMutableArray will be saved in NSUserDefaults. This NSMutableArray than will be used in another view where I have some UIImageView (1,2,3,4 and 5). Now when I will get the string values from that Array(f1,f2,f3). If it is f1 then an image will be assigned to UIImage 1 if it is f3 then to image 3 and so on.
实际上,我有 5 个按钮,单击每个按钮都会向 NSMutableArray 添加一个 NSString 值(值为:f1、f2、f3、f4 和 f5)。现在取决于用户是否在一天内点击 2 个或 3 或 5 个按钮。现在所有这些值都将保存在 NSMutableArray 中(可以是 1 或 2 但不超过 5)。NSMutableArray 将保存在 NSUserDefaults 中。这个 NSMutableArray 将在另一个视图中使用,其中我有一些 UIImageView(1、2、3、4 和 5)。现在,当我从该 Array(f1,f2,f3) 中获取字符串值时。如果它是 f1 那么一个图像将被分配给 UIImage 1 如果它是 f3 然后给图像 3 等等。
How to achieve this?
如何实现这一目标?
回答by Nicolas Bachschmidt
I would do something like that:
我会做这样的事情:
NSArray *array = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
NSString *a = nil, *b = nil, *c = nil, *d = nil, *e = nil;
NSUInteger idx = 0;
for ( NSString *string in array )
{
switch ( idx++ ) {
case 0: a = string; break;
case 1: b = string; break;
case 2: c = string; break;
case 3: d = string; break;
case 4: e = string; break;
}
}
回答by Antonio MG
As at least one element will be there in your array:
因为您的数组中至少会有一个元素:
NSString *a = (NSString *)[nameArray objectAtIndex:0];
As maximum will be five elements:
最多将有五个元素:
for(int i = 1;i<[array count];i++)
{
if(i == 1)
{
NSString *b = (NSString *)[nameArray objectAtIndex:1];
}
else if(i == 2)
{
NSString *c = (NSString *)[nameArray objectAtIndex:2];
}
else if(i == 3)
{
NSString *d = (NSString *)[nameArray objectAtIndex:3];
}
else if(i == 4)
{
NSString *e = (NSString *)[nameArray objectAtIndex:4];
}
}
回答by MrTJ
a = [nameArray objectAtIndex:0];
b = [nameArray objectAtIndex:1];
cd = [nameArray objectAtIndex:2];
If you want to put your array elements into separate variables with distinct names, there is no automation in objective c (unlike say, in JavaScript) since it is a compiled and not a interpreted language. Something similar you can achieve with NSDictionary, i.e. to "index" objects with strings or whatever type you want.
如果您想将数组元素放入具有不同名称的单独变量中,objective c 中没有自动化(不像在 JavaScript 中),因为它是一种编译语言而不是解释语言。您可以使用 NSDictionary 实现类似的功能,即使用字符串或您想要的任何类型“索引”对象。
回答by MrTJ
You could go on with a simple C array of 5 unsigned chars, where the index of the array would point to your data. Something like this:
您可以继续使用包含 5 个无符号字符的简单 C 数组,其中数组的索引将指向您的数据。像这样的东西:
unsigned char nameArray[5] = {0, 0, 0, 0, 0};
// if you want to set the 3rd variable, use:
nameArray[2] = 1;
// to query:
if (nameArray[2]) { ... }
// When you need to save it to NSUserDefaults, wrap it into an NSData:
NSData* nameData = [NSData dataWithBytes:nameArray length:sizeof(nameArray)];
[[NSUserDefaults standardUserDefaults] setObject:nameData forKey:@"myKey"];
// To query it:
NSData* nameData = [[NSUserDefaults standardUserDefaults] dataForKey:@"myKey"];
const unsigned char* nameArray2 = [nameData bytes];
unsigned char second = nameArray2[2];
EDITED: corrected array access
编辑:更正的数组访问
回答by user1253976
NSString *str1=nil;
NSString *str2=nil;
NSString *str3=nil;
NSString *str4=nil;
for (LocationObject *objLoc in arrLocListFirstView)
{
if (objLoc.isLocSelected)
{
for (LocationObject *obj in arrLocListFirstView)
{
if (str1 == nil)
str1 = objLoc.strLoc;
else if (str2 == nil)
str2 = obj.strLoc;
else if (str3 == nil)
str3 = obj.strLoc;
else if (str4 == nil)
str4 = obj.strLoc;
}
NSString *combined = [NSString stringWithFormat:@"%@,%@,%@,%@", str1,str2,str3,str4];
lblLocation.text=combined; (displaying text in UILabel)
}
}
回答by Conor
If there is at most five options the easiest way to do it is with an if-else-if chain.
如果最多有五个选项,最简单的方法是使用 if-else-if 链。
NSString *label1 = nil, *label2 = nil, *label3 = nil, *label4 = nil, *label5 = nil;
for (NSString *s in nameArray) {
if (label1 == nil)
label1 = s;
else if (label2 == nil)
label2 = s;
else if (label3 == nil)
label3 = s;
else if (label4 == nil)
label4 = s;
else if (label5 == nil)
label5 = s;
}