簡體   English   中英

如何將NSArray字符串對象值分配給其他NSString變量

[英]How to assign the NSArray string object values to other NSString variables

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);
}

上面的代碼顯示了nameArray的所有值。 但我想將所有這些值分配給這些NSString:

NSString *a;
NSString *b;
NSString *cd;

一個ray可以包含1個或多個元素,但不能超過5個。

實際上,我有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,依此類推。

如何實現呢?

我會做這樣的事情:

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;
    }
}

由於數組中至少有一個元素:

 NSString *a = (NSString *)[nameArray objectAtIndex:0];

最多將包含五個元素:

 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];
      }
 }
a = [nameArray objectAtIndex:0];
b = [nameArray objectAtIndex:1];
cd = [nameArray objectAtIndex:2];

如果要將數組元素放入具有不同名稱的單獨變量中,則目標c中不會實現自動化(與JavaScript中不同),因為它是編譯的而不是解釋的語言。 使用NSDictionary可以實現類似的效果,即使用字符串或所需的任何類型“索引”對象。

您可以繼續使用一個由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];

編輯:更正了數組訪問

              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)
                }

            }

如果最多有五個選項,最簡單的方法是使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM