get NSDicitionary value as NSString - ios

I am fetching user-id from Instagram JSON API.
It return only one value as Dictionary valueForKeyPath.
How I could convert it to string. I need to use the userid with some operations on Instagram.
I tried to convert it by this way
NSString *userId = [NSString stringWithFormat:#"%#", [dictionary valueForKeyPath:#"id"]];
The URL is coming not like string and I can't reconnect to Instagram with url like below:
URL: https://api.instagram.com/v1/users/(
123456 )/media/recent?access_token=567890.asasasddsdsdsdsdsadsdsdasd

I think you can try to get the value from the array. I guess it at index 0
NSString *userId = [NSString stringWithFormat:#"%#", [dictionary valueForKeyPath:#"id"][0]];
or
NSString *userId = [NSString stringWithFormat:#"%#", [[dictionary valueForKeyPath:#"id"] objectAtIndex:0]];
because if the array is nil the program will crash with the first code. Let me know if it worked
As #Paulw11 said you don't need the stringWithFormat because string is in the array
the line of code should be
NSString *userId = [[dictionary valueForKeyPath:#"id"] objectAtIndex:0];
Happy Coding!

Related

Taking data argument in a created NSString

How can I send data argument in an already created NSString. I have an NSDictionary:
NSArray *objectsArray=[[NSArray alloc]initWithObjects:#"item id %i",#"random id %i",nil];
NSArray *keysArray=[[NSArray alloc]initWithObjects:#"item",#"random",nil];
NSDictionary *dataDictionary=[[NSDictionary alloc]initWithObjects:objectsArray forKeys:keysArray];
Somewhere down the code I ask for the object in the NSDictionary
NSString *counterString=[dataDictionary objectForKey:#"random"];
How Can I now pass data argument into this retrieved NSString?
so my final string looks like
random id 67
NSString *finalString = [NSString stringWithFormat:counterString, digit];
If you also need to store it in your dictionary add
dataDictionary[#"random"] = finalString;
NSArray *objectsArray=[[NSArray alloc]initWithObjects:#"item id %i",#"random id %i",nil];
NSArray *keysArray=[[NSArray alloc]initWithObjects:#"item",#"random",nil];
NSDictionary *dataDictionary=[[NSDictionary alloc]initWithObjects:objectsArray forKeys:keysArray];
NSString *counterString=[dataDictionary objectForKey:#"random"];
NSString *final = [NSString stringWithFormat:counterString, 67];
dataDictionary[#"random"] = final;

iOS: Create String from Variable and string combined

I need to concatenate a string and a variable together - I kind of find lots of examples of adding a string prior to a variable but not the other way round - how do I do this?
NSString *theImage = [[self.detailItem valueForKey:#" resimagefiletitle"] description], #"'add on the end";
Something like this:
NSString *theImage = [NSString stringWithFormat:#"%# %#",[self.detailItem valueForKey:#" resimagefiletitle"], #"'add on the end"];
Or:
NSString *theImage = [NSString stringWithFormat:#"%# add on the end",[self.detailItem valueForKey:#" resimagefiletitle"]];
Try this
NSString *theImage = [NSString stringWithFormat:#"%# '>",[[self.detailItem valueForKey:#"resimagefiletitle"] description]];
Here I am considering [[self.detailItem valueForKey:#"resimagefiletitle"] description] gives NSString
We can concat diffrent type of datatypes into string by mention the format for it.
like if your want to concat two or more strings together then you can use the following code:
NSString *NewString = [NSString stringWithFormat:#"%#%#",#"This Is",#"way to concate string"];
and if your want concat integer value then you can mention the data format for it "%i".
eg:
int OutOf = 150;
NSString *NewString = [NSString stringWithFormat:#"%#%i",#"I got 100 out of ",OutOf];
this may help you.

Incompatible pointer types NSString and NSArray why?

-(void)displayData:(NSString *)text{
NSLog(#"DATA SEND");
NSString *string1 = [NSString stringWithFormat:text];
NSString *separate = [string1 componentsSeparatedByString:#"B"];
separate is the one that gives issue? How do I properly do this? I'm trying to perform a string split.
componentsSeparatedByString: method returns NSArray not NSString, try that:
NSArray *seperate = [string1 componentsSeparatedByString:#"B"];
what is that?
NSString *string1 = [NSString stringWithFormat:text];
Right way
NSString *string1 = [NSString stringWithFormat:#"%#",text];
or
NSString *string1 = [NSString stringWithString:text];
and then Grag answer.

iphone - app crash in a form

In my app, I have a view where user have to fill a form. But, sometime the app crash here, in this function, that simple cacth the value field and built a url to give
-(NSString*)urlToUpload{
NSString *string1 =[[NSString alloc]init];
string1= [NSString stringWithFormat:#"?nombre="];
NSString *string2 = [string1 stringByAppendingString:nameAdded];
//crash here
NSString *string3 = [string2 stringByAppendingString:#"&horario="];
NSString *string4 = [string3 stringByAppendingString:horarioAdded];
NSString *string5 = [string4 stringByAppendingString:#"&info="];
NSString *string6 = [string5 stringByAppendingString:infoAdded];
NSString *string7 = [string6 stringByAppendingString:#"&offerta="];
NSString *string8 = [string7 stringByAppendingString:offertaAdded];
NSString *lat_string = [[[NSString alloc] initWithFormat:#"%f",locationToUpload2.latitude] autorelease];
NSString *lon_string = [[[NSString alloc] initWithFormat:#"%f",locationToUpload2.longitude] autorelease];
NSString *string9 = [string8 stringByAppendingString:#"&latitude="];
NSString *string10 = [string9 stringByAppendingString:lat_string];
NSString *string11 = [string10 stringByAppendingString:#"&longitude="];
NSString *string12 = [string11 stringByAppendingString:lon_string];
NSString *url1 = [NSString stringWithFormat:#"http://myserverside/mysql_up.php"];
NSString *url = [url1 stringByAppendingString:string12];
return url;
}
EDIT:
It seems problem appers on nameAdded when there is a white space into textField(i.e. MisterB not crash, Mister B yes ).
But I am using:
nameAdded =[[nameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and NSLOg give of nameAdded is Mister%20B.
The crash still appearing...
Just use a single stringWithFormat::
- (NSString *)urlToUpload {
NSString *url = [NSString stringWithFormat:#"http://myserverside/mysql_up.php?nombre=%#&horario=%#&info=%#&offerta=%#&latitude=%f&longitude=%f",
[nameAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[horarioAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[infoAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[offertaAdded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
locationToUpload2.latitude, locationToUpload2.longitude];
return url;
}
Make sure the referenced variables are valid.
In your original code there is no need to alloc/init an NSString then assign another string to the same variable. That's a memory leak (string1).
If you really want to structure your code the way you have it, at least use an NSMutableString and append to that one mutable string. Creating over a dozen NSString variables is the wrong way to do it.
Updated: Ensure each of the strings added to the URL are properly escaped.
It looks like nameAdded may be the cause of your problems. Is it nil at that point?
Also
You are allocating a string, setting it to string1 and then immediately setting string1 to the class function stringWithFormat which allocates another string. Also you are using stringWithFormat but you aren't using any format so you could simply use NSString *string1 = #"?nombre=";
Rather than declaring all of those variables you should just use NSMutableString and build it all in one variabl

remove append strings

I have an nsarray with strings like this,
albumname/song42.mp3
albumname/song43.mp3 etc .
I want to remove the string "album name" and ".mp3" from the above array,and display it in a tableview as follows ,
song42
song43
then in the DidSelectRow ,i want to add the string
"http://www.domain.com/albumname/" and ".mp3" to the indepath.row element .
fo eg :
if user selects song42 in tableview ,then it must create a string like this "http://www.domain.com/albumname/song42.mp3"
How to do this ?
- [NSString stringByDeletingPathExtension] <--- This to remove
+ [NSString stringWithFormat:] <--- And this to recreate
EDIT My mistake, you first need to do this before you call the first method:
NSString *lastPath = [string lastPathComponent]; //song43.mp3
NSString *tableString = [lastPath stringByDeletingPathExtension]; //song43
You can use lastPathComponent and stringByDeletingPathExtension methods:
NSMutableArray *songs = [NSArray arrayWithCapacity:[sourceArray count]];
for (NSString *filename in sourceArray) {
[songs addObject:[[filename lastPathComponent] stringByDeletingPathExtension]];
}
use the String method componentsSeparatedByString first separate the strings using / and then . and discard what you don't need.
NSString *str="albumname/song42.mp3";
NSArray *mainStrArray = [str componentsSeparatedByString:#"/"];
NSArray remainingStrArray=[[mainStrArray objectAtIndex:1]componentsSeparatedByString:#"."];
NSString *result=[remainingStrArray objectAtIndex:0]; //here you have song42
You can use, as :
NSString *str = #"album name/song43.mp3";
str=[[str componentsSeparatedByString:#"/"][1]componentsSeparatedByString:#".mp3"][0];
NSLog(#"->%#",str); //song43
NSString *file = #"albumname/song42.mp3";
NSString *name = [[[[file componentsSeparatedByString:#"/"] objectAtIndex:1] componentsSeparatedByString:#"."] objectAtIndex:0];

Resources