Apidata in Array but array data not show in textbox - ios

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

Related

How to store value in array?

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

Unable to retrieve the data from Dictionary

In my project I am getting response from the server in the form
response:
<JKArray 0x7fa2e09036b0>(
{
id = 23;
name = "Name1";
},
{
id = 24;
name = "Name2";
}
)
From this response array i am retrieving the objects at different indexes and then adding them in a mutableArray and then into a contactsDictionary.
self.contactsDictionary = [[NSMutableDictionary alloc] init];
for(int i=0 ; i < [response count] ; i++)
{
NSMutableArray *mutableArray=[[NSMutableArray alloc] init];
[mutableArray addObject:[response objectAtIndex:i]];
[self.contactsDictionary setObject:mutableArray forKey:[NSString stringWithFormat:#"%i",i]];
}
I want to retrieve data for Key #"name" from the contactsDictionary at some other location in the project. So how to do it.
Thanks in advance....
this is the wrong way like you are setting your contactsDictionary.
replace below line
[self.contactsDictionary setObject:mutableArray forKey:[NSString stringWithFormat:#"%i",i]];
with
[self.contactsDictionary setObject:[mutableArray objectAtIndex :i] forKey:[NSString stringWithFormat:#"%i",i]];
becuase everytime your array have new objects so your contacts dictionary's first value have one object then second value have two object. so you shouldn't do that.
now, if you want to retrieve name then call like
NSString *name = [[self.contactsDictionary objectForKey : #"1"]valueForKey : #"name"];
avoid syntax mistake if any because have typed ans here.
Update as per comment:
just take one mutablearray for exa,
NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr addObject : name]; //add name string like this
hope this will help :)
Aloha from your respond I can give you answer Belo like that according to you response.
for(int i=0;i<[arrRes count];i++);
{
NSString *strId = [NSString stringWithFormat:#"%#",[[arrRes obectAtIndex:i]objectForKey:#"id"]];
NSString *StrName = [NSString stringWithFormat:#"%#",[[arrRes objectAtIndex:i]objectForKey:#"name"]];
NSLog(#"The ID is -%#",strId);
NSLog(#"The NAME is - %#",strName);
}

How can I show all of objects in NSMutable Array on text field?

I want to show all objects in mutable array on to textfield, label, something else except NSLog
- (IBAction)purchasePressed:(id)sender {
NSMutableArray *addItem = [[NSMutableArray alloc] init];
[addItem addObject:#"Almond"];
[addItem addObject:#"Choc"];
"number" is my label (I'm not sure that all of Objects in MutableArray can be showed on textfield or not?) i can do it only with NSLog.
for (i = 0;i < [addItem count] ; i++ )
{
NSLog(#"%#", addItem);
NSString *test1=(#"%#", addItem);
number.text=test1;
}
Every time you set the text of a label you replace the previous text.
Try replacing your whole loop with something like:
number.text = [addItem componentsJoinedByString:#", "];
Which will create a single string from all of the strings in the array and add that to the label. You could do something similar in your loop if you want to.
If you want a string with all the values concatenated:
NSString *mainString = [NSString alloc] init];
for (NSString *item in addItem) {
mainString = [mainString stringByAppendingString:item];
}
number.text = mainString;
EDIT: Using NSMutableString
NSMutableString *mainString = [[NSMutableString alloc] init];
for (NSString *item in addItem) {
[mainString appendString:item];
}
number.text = mainString;

iOS: Display NSArray of NSStrings in a label with special character encoding

I have several NSStrings which I add to an NSArray. The string may contain special characters. In the end I want to print the array to a UILabel.
The very simplified code (if you think I missed something, let me know):
NSString *strBuffer = #"Röckdöts";
NSLog(#"String: %#", strBuffer);
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSLog(#"Array: %#", marrSelectedStrings);
NSUInteger iCount = [marrSelectedStrings count]
for (NSUInteger i = 0; i < iCount; i++)
{
NSLog(#"element %d: %#", i, [marrSelectedStrings objectAtIndex: i]);
}
In a different UIViewController:
self.label.text = [NSString stringWithFormat:#"%#", marrSelectedStrings];
The string itself prints out fine. For the array however, it depends on the output method, whether the console diplays the correct special character or code for it. The label only prints code instead of the real characters. The print out via NSLog looks like the following:
Buffer: Röckdöts
Array: (
R\U00f6ckd\U00f6ts
)
element 0: Röckdöts
Whereas the label reads:
R\U00f6ckd\U00f6ts
I tried using stringWithUTF8String during the adding to the array as well as encoding during assigning it to the label like so, but it didn't change the result:
// adding with UTF8 encoding
[marrSelectedStrings addObject:[NSString stringWithUTF8String:[strBuffer UTF8String]]];
// printing to label with UTF8 encoding
self.label.text = [NSString stringWithUTF8String:[[NSString stringWithFormat:#"%#", marrSelectedStrings] UTF8String]];
Is there an easier way to simply print the array with correct character encoding to the UILabel than iterating over the array and appending every single word?
Try this
NSString * result = [[marrSelectedStrings valueForKey:#"description"] componentsJoinedByString:#""];
self.label.text = result;
try like this
NSMutableArray *marrSelectedStrings = [[NSMutableArray alloc] init];
[marrSelectedStrings addObject:strBuffer];
NSString *description = [marrSelectedStrings description];
if (description) {
const char *descriptionChar = [description cStringUsingEncoding:NSASCIIStringEncoding];
if (descriptionChar) {
NSString *prettyDescription = [NSString stringWithCString:descriptionChar encoding:NSNonLossyASCIIStringEncoding];
if (prettyDescription) {
description = prettyDescription;
}
}
}
NSLog(#"Array: %#", description);

Converting an NSArray component into an integer or decimal number

I have a case where the data read from the CSV file in the app has to be converted into an integer, has to be plotted later. Currently it doesn't recognize when the data is saved as
int i=[[rows objectAtIndex:0] componentsSeparatedByString:#","];
This is the implemented code.
-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{
[self serverConnect];
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(response);
NSString *stripped1 = [response stringByReplacingOccurrencesOfString:#"\r" withString:#""];
NSArray *rows = [stripped1 componentsSeparatedByString:#"\n"];
NSArray *components;
for (int i=0;i<[rows count]; i++) {
if(i == 0 || [[rows objectAtIndex:i] isEqualToString:#""]){
continue;
}
components = [[rows objectAtIndex:i] componentsSeparatedByString:#","];
NSLog(#"data1:%# data2:%# data3:%#", [components objectAtIndex:0] ,[components objectAtIndex:1],[components objectAtIndex:2]);
}
data1, data2 and data3 are supposed to be integers.
Thanks a lot.
componentsSeparatedByString returns substrings, or instances of NSString.
components = [[rows objectAtIndex:i] componentsSeparatedByString:#","];
You just need to take each member of 'components' and get it's intValue, like so:
int myInt = [[components objectAtIndex:n] intValue];
NSArray and NSMutableArray can only contains objects. So get the integer value from it, use [object intValue]. If you need to add an integer to an array, create a NSNumber object from the integer and insert it. I know Rayfleck answered your question and i just want to point out the way how array works in iOS. Hope this helps.

Resources