I am adding multiple elements in NSMutableArray but its storing single element at o index. I need to add the elements into next index every time until the end of the loop
for (i=1; i<58; i++) {
NSString *stringFromInt = [NSString stringWithFormat:#"%d", i];
NSString *strlastName =[[dict objectForKey:stringFromInt]objectAtIndex:7];
[arrLastName insertObject:strlastName atIndex:i];
}
This particular code should throw NSRangeException. You can't insert the item at index greater than array count minus one.
I think you want to add elements to array untill the loop runs, The problem might be with i, which is not initialized
for (int i=1; i<58; i++) {
NSString *stringFromInt = [NSString stringWithFormat:#"%d", i];
NSString *strlastName =[[dict objectForKey:stringFromInt] objectAtIndex:7];
[arrLastName insertObject:strlastName atIndex:i]; // or try [arrLastName addObject:strlastName];
}
Hope this helps
Related
lookupAllForSQL is a database function.
The table name and identifier are dynamic.
for (int i=0; i<FavIdent.count; i++)
{
NSString *strfavarry = [NSString stringWithFormat:#"SELECT Title FROM %# WHERE identifire='%#'",[FavTablename objectAtIndex:i],[FavIdent objectAtIndex:i]];
FavTitle = [FavData lookupAllForSQL:strfavarry];
}
FavTitle should retain only last row found, but I want all the indicated columns.
lookupAllForSQL is returning an array, so the value you store in FavTitle (which should be favTitle - variables start with a lower case letter by convention) is being replaced each time, resulting in the last value being left after the loop exits.
You need to use an NSMutableArray and append the sub-array each time through the loop:
NSMutableArray *tempArray = [NSMutableArray new];
for (int i=0; i<FavIdent.count; i++)
{
NSString *strfavarry = [NSString stringWithFormat:#"SELECT Title FROM %# WHERE identifire='%#'",[FavTablename objectAtIndex:i],[FavIdent objectAtIndex:i]];
[tempArray addObjectsFromArray:[FavData lookupAllForSQL:strfavarry]];
}
favTitle = [tempArray copy]; // Convert it back to an NSArray
You should also use prepared SQL statements rather than string interpolation to guard against SQL injection.
In your current code you are assigning the result array from [FavData lookupAllForSQL:strfavarry] to FavTitle in the for loop. So FavTitle must always have last result array in it when the for loop executed.
use below code
NSArray *result = [FavData lookupAllForSQL:strfavarry];
if(result){
[FavTitle addObject:[result objectAtIndex:0]];
}
Please suggestion only for NSArray.
NSArray *addressAry = [[arr1 valueForKey:#"var_offline_address"] componentsSeparatedByString:#"$#$"];
for (int i = 0; i<=addressAry.count; i++) {
NSString *str = [addressAry objectAtIndex:i];
if (str containsString:#"NA") {
NSString *strChange;
strChange = #"Address Not Available";
[myMutableArray insertObject:myObject atIndex:42];
To add an object to the front of the array, use 0 as the index:
[myMutableArray insertObject:myObject atIndex:0];
Use NSMutableArray. It has a method called:
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index
Like:
[yourMutableArray insertObject: theObject atIndex: theIndex];
As it describes:
If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.
For more info see the official doc.
I'm doing an RSVP reading project app where it blinks words on the screen. You can set the word chunk size (how many words you want displayed at a time) to either 1, 2, or 3. I got it working for 1 word by having my paragraph in a string and doing:
[self.textInput componentsSeparatedByString:#" ";
This makes me an array of words that I can use to blink one word at a time. How would I be able to do this with displaying 2 words at a time? Is there a way I can use this function again to do it differently, or should I iterate over this word array and make a new one with 2 word strings?
Any help or advice would be greatly appreciated as to what the best practice would to get this done. Thanks.
just like keith said create an array
NSArray *allwordsArray = [self.textInput componentsSeparatedByString:#" "];
Now you got all the info you need. Meaning you got the array with every word in it. Now its just a matter of putting it together. (I haven't tested this code)
NSMutableArray *twoWordArray = [[NSMutableArray alloc] init];
int counter=0;
for (int i=0; i<[allwordsArray count]; i++)
{
if (counter >= [allwordsArray count]) break;
NSString *str1 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
counter++;
if (counter >= [allwordsArray count]) break;
NSString *str2 = [NSString stringwithformat#"%#", [allwordsArray objectAtIndex:counter]];
NSString *combinedStr = [NSString stringwithformat#"%# %#", str1,str2];
[twoWordArray addObject: combinedStr];
counter++;
}
You have broken the string into components, which is on the right track. You could then make a smaller array that only includes components until you reach the chunk size. The final step would be to rejoin the string.
NSArray *components = [self.textInput componentsSeparatedByString:#" "];
NSRange chunkRange = NSMakeRange(0, chunkSize);
NSArray *lessComponents = [components subarrayWithRange:chunkRange];
NSString *newString = [lessComponents componentsJoinedByString:#" "];
Well this is my code:
NSDictionary *dicto;
NSString *ndccode;
NSString *string=#" ";
for (int i=0; i<array.count; i++) {
dicto=[array objectAtIndex:i];
ndccode=[dicto objectForKey:#"NDCCode"];
string=[string stringByAppendingString:ndccode];
string=[string stringByAppendingString:#"\n"];
NSLog(#"%#",string);
}
NSLog(#"%#",string);
In the above code, i have values in dicto nsdictionary which loops one by one value and assigns to ndccode which is string. Then I am adding to string so that I can append it to the next line.
output :
name1
name2
name3
.....
Instead of assigning to the string. I want to assign it to an array.Could you please tell me how to assign it to an array for this example,since it is in loop. I want to use the array for later purposes.
Thank you in advance.
NSDictionary *dicto;
NSString *ndccode;
NSMutableArray *outputArray=[NSMutableArray new];
for (int i=0; i<array.count; i++) {
dicto=[array objectAtIndex:i];
ndccode=[dicto objectForKey:#"NDCCode"];
[outputArray addObject:ndccode];
}
or even more succinctly
NSArray *outputArray = [array valueForKey:#"NDCCode"];
My problem is that when I use fast enumeration to load objects from my array, like so:
for(SetOfObjects *set in _myArray){
NSLog (#"%#"[set anObject];
}
It will print out my specified object without a problem, however when it comes time to assign these objects to an NSArray of labels. The last object returns as 0.
Like so:
for(SetOfObjects *set in _myArray){
for(UILabel *label in _arrayOfLabels){
int i = [set intObject];
NSString *string = [NSString stringWithFormat:#"%i",i];
label.text = string;
}
}
I think, I have gone wrong here. The code works, but the problem is that all labels are then set as 0.
Any tips welcome.
You are iterating the labels within each SetOfObjects instance, when in fact you want to iterate both arrays at the same time, which cannot be done using fast enumeration.
Instead revert to indexed-access of both arrays:
NSInteger count = [_myArray count];
NSAssert([_arrayOfLabels count] == count, #"Different array sizes!");
for (NSInteger index = 0; index < count; index++) {
SetOfObjects *set = _myArray[index];
UILabel *label = _arrayOfLabels[index];
int i = [set intObject];
NSString *string = [NSString stringWithFormat:#"%i",i];
label.text = string;
}
Note the assertion to check that both arrays are the same size.
EDIT: Oops, i was a bad variable name to choose for the index...