|
|
|
iOS Objective-C : How to create NSMutableArray Dynamically? |
|
|
|
|
|
|
|
Code (Objective-C)
NSMutableDictionary *myArrays;
NSMutableArray *newArray = [[NSMutableArray alloc] init];
[myArrays addObject:newArray forKey:someLabel1.text];
[myArrays addObject:newArray forKey:someLabel2.text];
[myArrays addObject:newArray forKey:someLabel3.text];
|
|
|
|
|
Date :
2015-01-08 11:20:40 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sorry#1 wrong mistake. You can create NSMutableArray item in NSMutableDictionary
Code (Objective-C)
/*** Create JSON ***/
// A dictionary object
NSDictionary *dict;
NSMutableArray *myObject;
// Define keys
NSString *memberid;
NSString *name;
NSString *tel;
// Define keys
memberid = @"MemberID";
name = @"Name";
tel = @"Tel";
// Create array to hold dictionaries
myObject = [[NSMutableArray alloc] init];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"1", memberid,
@"Weerachai", name,
@"0819876107", tel,
nil];
[myObject addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"2", memberid,
@"Win", name,
@"021978032", tel,
nil];
[myObject addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"3", memberid,
@"Eak", name,
@"087654321", tel,
nil];
[myObject addObject:dict];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myObject
options:NSJSONWritingPrettyPrinted
error:nil];
// Resutl
// [{ "MemberID":"1", "Name":"Weerachai", "Tel":"0819876107" }, { "MemberID":"2", "Name":"Win", "Tel":"021978032" }, { "MemberID":"3", "Name":"Eak", "Tel":"087654321" }]
/*** Read JSON ***/
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
NSString *strMemberID = [dataDict objectForKey:@"MemberID"];
NSString *strName = [dataDict objectForKey:@"Name"];
NSString *strTel = [dataDict objectForKey:@"Tel"];
NSLog(@"MemberID = %@",strMemberID);
NSLog(@"Name = %@",strName);
NSLog(@"Tel = %@",strTel);
NSLog(@"====================");
}
|
|
|
|
|
Date :
2015-01-08 11:22:13 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thanks mr.win.
I will try this method
|
|
|
|
|
Date :
2015-01-08 11:44:52 |
By :
vishwa |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|