How to store value in array? - ios

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

Related

How can I change substring(s) in an array of strings in Objective-C?

I have a weird error with decoding & sign so its always shown as &, so && will be &&, a&b is shown as a&b etc.
I have those weird characters in my array of strings sometimes, so I want to remove them. To be more clear, if I have an array like this:
"str1", "a&b", "12345", "d&&emo&"
I want to it to be:
"str1", "a&b", "12345", "d&&emo&"
So all & should be changed into just &.
This is the code I tried, but it didn't help. Elements are not changing their values:
for (int i = 0; i <= self.array-1; i++)
{
NSLog(#"%#", self.array[i]);
[self.array[i] stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
NSLog(#"%#", self.array[i]);
}
I am new to Objective-C.
NSString instances are immutable, which means you can't change them once they are created. The stringByReplacingOccurencesOfString:withString: method actually returns a new NSString instance. You could use NSMutableString, or alternatively, you can create a new array containing the replacement strings, e.g.:
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *input in self.array)
{
NSString *replacement = [input stringByReplacingOccurrencesOfString:#"&" withString:#"&"];
[newArray addObject:replacement];
}
self.array = newArray;

Apidata in Array but array data not show in textbox

NSString *stringForTextView = #"";
for(NSDictionary *d in _arr1)
{
stringForTextView = [NSString stringWithFormat:#"CellId:%#\nLacId:%#\nLattitude:%#\nLongitude:%#\nDistance:%#\nAddress:%#\n", [d valueForKey:#"cid"],[d valueForKey:#"lac"],[d valueForKey:#"lat"], [d valueForKey:#"long"], [d valueForKey:#"distance"],[d valueForKey:#"address"]];
}
txtcellview.text = stringForTextView;
only last record show....all record not show in textview
apidata in arr but arry data not show in textbox
"only last record show...."
That's because you are constantly assigning the same variable, each iteration of the loop and so whatever was assigned last will remain.
I'm not certain what you want, but you probably want to keep appending each element to a string:
NSMutableString *stringForTextView = [NSMutableString new];
for(NSDictionary *d in _arr1)
{
[stringForTextView appendString:[NSString stringWithFormat:#"CellId:%#\nLacId:%#\nLattitude:%#\nLongitude:%#\nDistance:%#\nAddress:%#\n", [d valueForKey:#"cid"],[d valueForKey:#"lac"],[d valueForKey:#"lat"], [d valueForKey:#"long"], [d valueForKey:#"distance"],[d valueForKey:#"address"]]];
}
txtcellview.text = stringForTextView;
Note that this code does not add a delimiter between elements; I'll leave that to you...

Insert elements in next index of NSMutableArray

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

How to add values or add object into NSArray in Objective C using for loop?

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"];

Loading NSMutableArray objects into an IBCollection of Labels

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...

Resources