How I can create a NSMutableArray (name) dynamically by a NSString value?
e.g. NSString *stringName = #"helloArray1";
Then create the NSMutableArray with the "helloArray1" dynamically.
e.g. NSMutableArray * (--here the stringName / helloArray1---) = [NSMutableArray new];
And then the NSLog:
NSLog(#"%#", (--here the stringName / helloArray1---) );
Thanks for your help
I think that's not possible.
Better is set dynamically all into object. For example:
NSMutableDictionary *variables = [NSMutableDictionary new];
for (int i = 0; i < 10; i++) {
NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:#"1", #"2", nil];
[variables setObject:temp forKey:[NSString stringWithFormat:#"value_%d", i]];
}
NSLog(#"%#", [variables objectForKey:#"value_1"]);
Related
NSArray has 3 values
#[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"]
And I want to put this array into the UILabel with word wrap.
So when I run, it should show like this in label.
addd:etyrhetwrwr
fdfdd:jjjjhhhh
fsuy:jjhwgggggg
But I can't separate this NSArray. How can I do that?
NSArray *myArray = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString *labelString = [myArray componentsJoinedByString:#"\n"];
Then use the labelString to set your label text property.
Try This
NSArray *yourArray = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString *string = [myArray componentsJoinedByString:#"\n"];
[yourLabel setText:string];
You can separate NSArray in UILabel by
NSArray myArr = #[#"addd:etyrhetwrwr", #"fdfdd:jjjjhhhh", #"fsuy:jjhwgggggg"];
NSString strLabel = [myArr string:#"\n"];
try this code
NSArray *array = [[NSArray alloc] initWithObjects:#"adad",#"fgfdgdfg",#"sddgfs", nil];
NSMutableString *strFinalData = [[NSMutableString alloc] init];
for (int i=0; i<[array count]; i++)
{
[strFinalData appendFormat:#"%# ",[array objectAtIndex:i]];
}
NSLog(#"Final String: %#",strFinalData);
Now set yourLabel.text = strFinalData;
I have a twodimensional array where i i would like to print all second values of the objects into an UIlabel homePlayersFouls. the problem is i do not know how to this.
I have tried following things:
componentsJoinedByString:#"\n".
This will just print (
The other thing ive tried is this:
[[homePlayersArray objectAtIndex:i] objectAtIndex:1];
this just prints 0 since it looping and deleting the content inside label
i've checked wether there is something wrong the the array, but when i do this inside the loop:
nslog(#"%#",[[homePlayersArray objectAtIndex:i] objectAtIndex:1];);
It prints all 0 0 0 which is the 3 second values in my objects.
The question is then how can i print all my second values in all the objects in the array into an UIlabel?
here is the complete code:
for (int i=0;i<[homeNumbersArray count];i++){
NSArray *tempArray = [[NSArray alloc] initWithObjects:[homeNumbersArray objectAtIndex:i],[NSNumber numberWithInt:0], nil];
[homePlayersArray addObject:tempArray];
NSObject* someObject = [[homePlayersArray objectAtIndex:i] objectAtIndex:1];
homePlayersFouls.text = [NSString stringWithFormat:#"%#", someObject];
}
You need:
homePlayersFouls.text = [homePlayersFouls.text stringByAppendingString:[NSString stringWithFormat:#"%#", someObject]];
NSMutableString *fouls = [NSMutableString string];
for (int i=0;i<[homeNumbersArray count];i++){
NSArray *tempArray = [[NSArray alloc] initWithObjects:[homeNumbersArray objectAtIndex:i],[NSNumber numberWithInt:0], nil];
[homePlayersArray addObject:tempArray];
NSObject* someObject = [[homePlayersArray objectAtIndex:i] objectAtIndex:1];
[fouls appendFormat:#"%#", someObject];
}
homePlayersFouls.text = fouls;
I have an iPad that reads data from an SQL database. The following code works fine and retrieves 2 fields from each record and reads them into an NSArray.
I now need to read 5 of the fields and I can't help but think that there is a better way of doing it rather than running 5 separate queries through php (the getinfo.php file with the choice parameter set to pick the different fields).
Any pointers to a better method for doing this?
NSString *strURLClass = [NSString stringWithFormat:#"%#%#", #"http://wwwaddress/getinfo.php?choice=1&schoolname=",obsSchoolName];
NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsClass = [observationsArrayClass objectEnumerator];
NSString *strURLDate = [NSString stringWithFormat:#"%#%#", #"http://wwwaddress/getinfo.php?choice=5&schoolname=",obsSchoolName];
NSArray *observationsArrayDate = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLDate]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsDate = [observationsArrayDate objectEnumerator];
id className, dateOfObs;
while (className = [enumForObsClass nextObject])
{
dateOfObs = [enumForObsDate nextObject];
[observationListFromSQL addObject:[NSDictionary dictionaryWithObjectsAndKeys:className, #"obsClass", dateOfObs, #"obsDate",nil]];
}
Yes, you can do that with less code by "folding" the statements into a loop, and using a mutable dictionary:
// Add other items that you wish to retrieve to the two arrays below:
NSArray *keys = #[#"obsClass", #"obsDate"]; // Key in the dictionary
NSArray *choices = #[#1, #5]; // Choice in the URL string
NSMutableArray *res = [NSMutableArray array];
NSMutableArray *observationListFromSQL = [NSMutableArray array];
for (int i = 0 ; i != keys.count ; i++) {
NSNumber *choice = choices[i];
NSString *strURLClass = [NSString stringWithFormat:#"http://wwwaddress/getinfo.php?choice=%#&schoolname=%#", choice, obsSchoolName];
NSArray *observationsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
NSEnumerator *objEnum = [observationsArrayClass objectEnumerator];
NSString *key = keys[i];
NSMutableDictionary *dict;
if (res.count < i) {
dict = res[i];
} else {
dict = [NSMutableDictionary dictionary];
[res addObject:dict];
}
id item;
while (item = [objEnum nextObject]) {
[res setObject:item forKey:key];
}
}
I'm new to Objective-C and I'm trying to create a simple dictionary style app for personal use. Right now I'm attempting to make a loop that prints randomly selected NSArrays that have been added to an NSDictionary. I'd like to print each array only once. Here is the code I'm working with:
NSArray *catList = [NSArray arrayWithObjects:#"Lion", #"Snow Leopard", #"Cheetah", nil];
NSArray *dogList = [NSArray arrayWithObjects:#"Dachshund", #"Pitt Bull", #"Pug", nil];
...
NSMutableDictionary *wordDictionary = [[NSMutableDictionary alloc] init];
[wordDictionary setObject: catList forKey:#"Cats"];
[wordDictionary setObject: dogList forKey:#"Dogs"];
...
NSInteger keyCount = [[wordDictionary allKeys] count];
NSInteger randomKeyIndex = arc4random() % keyCount;
int i = keyCount;
for (i=i; i>0; i--) {
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
NSMutableArray *randomlySelectedArray = [wordDictionary objectForKey:randomKey];
NSLog(#"%#", randomlySelectedArray);
}
This code prints the same array "i" times. Any pointers on how to exclude previously printed arrays from being printed again?
I'm wondering if removeObjectForKey: could be of any use.
You just need to re-calculate the random key index every time you go through the loop, and then, as you suggest, use removeObjectForKey:.
Something like this:
NSArray *catList = [NSArray arrayWithObjects:#"Lion", #"Snow Leopard", #"Cheetah", nil];
NSArray *dogList = [NSArray arrayWithObjects:#"Dachshund", #"Pitt Bull", #"Pug", nil];
//...
NSMutableDictionary *wordDictionary = [[NSMutableDictionary alloc] init];
[wordDictionary setObject: catList forKey:#"Cats"];
[wordDictionary setObject: dogList forKey:#"Dogs"];
//...
while ([wordDictionary count] > 0) {
NSInteger keyCount = [wordDictionary count];
NSInteger randomKeyIndex = arc4random() % keyCount;
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
NSMutableArray *randomlySelectedArray = [wordDictionary objectForKey:randomKey];
NSLog(#"%#", randomlySelectedArray);
[wordDictionary removeObjectForKey: randomKey];
}
In your code, you generate a random randomKeyIndex, then use it without changing its value i times in the loop. So you get i times the same array.
NSInteger randomKeyIndex = arc4random() % keyCount;
// ...
for (i=i; i>0; i--) {
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
// ...
}
As you say removeObjectForKey is an option for you, you can change your code into something like this:
NSInteger keyCount = [[wordDictionary allKeys] count];
for (i=keyCount; i>0; i--) {
NSInteger randomKeyIndex = arc4random() % keyCount;
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
NSMutableArray *randomlySelectedArray = [wordDictionary objectForKey:randomKey];
[wordDictionary removeObjectForKey:randomKey];
keyCount--;
NSLog(#"%#", randomlySelectedArray);
}
I have four arrays as follows:
toArray = [[NSMutableArray alloc] initWithObjects:#"to 1",#"to 2",#"to 3",#"to 4",#"to 5",#"to 6",#"to 7",nil];
fromArray = [[NSMutableArray alloc] initWithObjects:#"from 1",#"from 2",#"from 3",#"from 4",#"from 5",#"from 6",#"from 7",nil];
messageArray = [[NSMutableArray alloc] initWithObjects:#"message 1",#"message 2",#"message 3",#"message 4",#"message 5",#"message 6",#"message 7",nil];
dayArray = [[NSMutableArray alloc] initWithObjects:#"day 1",#"day 2",#"day 3",#"day 4",#"day 5",#"day 6",#"day 7",nil];
I want to copy or create a single array which should contain all these 4 arrays.
How can i achieve it?
Your question is a little ambiguous; how do you want the result?
NSMutableArray *completeArray = [NSMutableArray array];
[completeArray addObjectsFromArray:toArray];
[completeArray addObjectsFromArray:fromArray];
[completeArray addObjectsFromArray:messageArray];
[completeArray addObjectsFromArray:dayArray];
Or
NSMutableArray *completeArray = [NSMutableArray arrayWithObjects:toArray, fromArray, messageArray, dayArray, nil];
Or if all arrays have the same number of elements:
NSMutableArray *completeArray = [NSMutableArray array];
for (NSUInteger i = 0; i < [toArray count]; i++)
{
NSString *fullString = [NSString stringWithFormat:#"%# %# %# %#", [toArray objectAtIndex:i], [fromArray objectAtIndex:i], [messageArray objectAtIndex:i], [dayArray objectAtIndex:i]];
[completeArray addObject:fullString];
}
Or, as an array of dictionaries (also assuming all arrays are same length):
NSMutableArray *completeArray = [NSMutableArray array];
for (NSUInteger i = 0; i < [toArray count]; i++)
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[toArray objectAtIndex:i], #"to",
[fromArray objectAtIndex:i], #"from",
[messageArray objectAtIndex:i], #"message",
[dayArray objectAtIndex:i], #"day",
nil];
[completeArray addObject:dict];
}
This should work:
NSArray *singleArray = [NSArray arrayWithElements:toArray, fromArray, messsageArray, dayArray, nil];
// mind the nil element at the end
NSMutableArray *result = [NSMutableArray arrayWithCapacity:0];
[result addObjectsFromArray:toArray];
[result addObjectsFromArray:fromArray];
[result addObjectsFromArray:messageArray];
[result addObjectsFromArray:dayArray];