使用循環多維數組我想爲iOS平臺像下面的PHP語法創建數組,非常感謝~~如何創建IOS的Xcode
$createArray = array();
for ($i = 0; $i<10; $i++) {
$createArray[$i]['name'] = $name;
$createArray[$i]['age'] = $age;
}
使用循環多維數組我想爲iOS平臺像下面的PHP語法創建數組,非常感謝~~如何創建IOS的Xcode
$createArray = array();
for ($i = 0; $i<10; $i++) {
$createArray[$i]['name'] = $name;
$createArray[$i]['age'] = $age;
}
儘量保存自己的價值觀NSDictionary中,並添加字典到你的陣列
NSMutableArray *theArray = [NSMutableArray array];
for (int indexValue = 0; indexValue<10; indexValue++) {
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:@"name"];
[theDictionary setObject:age forKey:@"age"];
[theArray addObject:theDictionary]
}
在檢索時間,
NSString *name = [[theArray objectAtIndex:indexValue] objectForKey:@"name"];
NSString *age = [[theArray objectAtIndex:indexValue] objectForKey:@"age"];
試試這個。
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 2; j++) {
//Do your Stuff
// [subArray addObject:name];
// [subArray addObject:Age];
}
[array addObject:subArray];
}
OR
爲什麼不能用
NSDictionary
您可以使用它。但這在IOS中不是更好的方式。
NSMutableArray *array[20];
for (int i=0;i< 20; i++)
{
array[i] = [NSMutableArray array];
for (int j=0;j<3;j++)
{
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:@"name"];
[theDictionary setObject:age forKey:@"age"];
[[array[i] addObject:theDictionary]
}
}
,你可能會發現:
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 8; j++) {
[subArray addObject:[NSNumber numberWithInt:0]];
}
[array addObject:subArray];
[subArray release];
}
還要檢查this question
首先你已經設置一個NSMutableDictionary上的.h文件
@interface MSRCommonLogic : NSObject
{
NSMutableDictionary *twoDimensionArray;
}
then have to use following functions in .m file
- (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
{
if(!twoDimensionArray)
{
twoDimensionArray =[[NSMutableDictionary alloc]init];
}
NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
[twoDimensionArray setObject:value forKey:strKey];
}
- (id)getValueFromArray :(int)rows cols:(int) col
{
NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
return [twoDimensionArray valueForKey:strKey];
}
考慮使用的NSDictionary比這更好2維陣列。讓我知道你是否需要關於NSDictionary的信息 –