Taking data argument in a created NSString - ios

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;

Related

string handling in ios

I am getting String data from server in the format below. I want to get the value for every tag like phonenumber and name etc. I am able to convert it in array by comma separator. how to get individual values?
Company:Affiliated CO,Organization:TECHNICAL EDUCATION
SOCIETY,Organization:SINHGAD,Organization:National Basketball Association,Person:Parikshit N. Mahalle,PhoneNumber:81 98 22 416 316,PhoneNumber:9120-24100154,Position:Professor,SportsEvent:NBA.
Say your original string is stored in rawString.
You need to :
1) split the string by ,
NSArray *pieces = [rawString componentsSeparatedByString:#","];
2) for each item in this array, split it by :, and add it to a dictionary :
NSMutableDictionary *dict = [NSMutableDictionary new];
for (NSString *piece in pieces) {
NSArray *splitPiece = [piece componentsSeparatedByString:#":"];
// key is at splitPiece[0], value is at splitPiece[1]
dict[splitPiece[0]] = splitPiece[1];
}
Then you'll have a dictionary of what you wanted in the first place.
But as suggested in the comments, it would be far better (and more flexible) for you to receive JSON data.
Edit: your original string shows there are multiple fields named Organization. The code I've given is not designed to handle such cases, it's up to you to build upon it.
If this data is not being returned as a JSON object then you'll have to go with #Clyrille answer. But if it is JSON then NSJSONSerialization:JSONObjectWithData:options:error: will be the way to go.
EXAMPLE
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:/*urlResponse*/ options:0 error:nil];
NSString *company = [json objectForKey:#"Company"];
NSString *Organization = [json objectForKey:#"Organization"];
NSString *Person = [json objectForKey:#"Person"];
NSString *PhoneNumber = [json objectForKey:#"PhoneNumber"];
NSString *Position = [json objectForKey:#"Position"];
NSString *SportsEvent = [json objectForKey:#"SportsEvent"];

How edit a NSString that contains a NSMutableDictionary key/value?

I have a NSDictionary and make a copy of NSDictionary:
NSDictionary *immutableDict = (NSDictionary *)json;
NSMutableDictionary *mutableDict = [immutableDict mutableCopy];
I want to convert this into a NSString selection category for then edit contain.
NSString * name = [mutableDict objectForKey:#"category"];
name = [name stringByAppendingString:[NSString stringWithFormat:#"mytexttoadd"]];
But when I want to change this NSString my app crash because the NSString is a NSCFArray ..
How can make this a NSString so I can edit it?

get NSDicitionary value as NSString

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!

JSon returning Integer values into array

I have successfully realized the interaction with json and returning successfully all the required entities. But i have one field that will be returned as an integer. I want to do the simplest thing and access an element of the array , which is an integer , and compare it with another integer. No matter what i do it doesn't work out : this is my code so far :
NSString *strURL2 = [NSString stringWithFormat:#"MyURL"];
NSData *dataURL2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL2]];
NSString *strResult2 = [[NSString alloc] initWithData:dataURL2 encoding:NSUTF8StringEncoding];
NSDictionary *json2 = [strResult2 JSONValue];
userExists = [json2 valueForKeyPath:#"userId"];
Where userExists is an array, and the values stored in it are NOT strings , they are Int.
Any help?!
NSString *strURL2 = [NSString stringWithFormat:#"MyURL"];
NSData *dataURL2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL2]];
NSString *strResult2 = [[NSString alloc] initWithData:dataURL2 encoding:NSUTF8StringEncoding];
NSDictionary *json2 = [strResult2 JSONValue];
Now if you want to add the integer to the array (has to be NSMutableArray of course) userExists, you do it like so:
[userExists addObject:[[json2 objectForKey:#"userId"] integerValue]]
If your intention was to compare the returned integer with an integer in the array at index i then you do it like so:
if([userExists objectAtIndex:i] == [[json2 objectForKey:#"userId"] integerValue]) {
....
}

iOS - get specific value from plist

I would like to get the value of NO (from the plist under) into a NSString i got. But I'm stuck.
I used the following code:
//Load Dictionary with wood name cross refference values for image name
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:#"Numbers" ofType:#"plist"];
NSDictionary *numberDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistCatPath];
self.numberArray = numberDictionary[#"Two"];
// ,[codeForCountryDictionary objectForKey:selectedCountryPicker]
number = [self.policeArray valueForKey:#"NO"];
NSLog(#"Numero: %#", number);
But then I got:
(
012
)
and I only want 012, not with the parentheses.
My Plist:
the "NO" is a NSString key in the "Two" NSDictionary and NSString "012" is the value, for the key "NO"
NSDictionary * dict = numberDictionary[#"Two"];
for (NSString * key in dict) {
NSLog(#"%# %#", key, dict[key]);
}
If you want to get the key for a value from NSDictionary, use
NSDictionary * dict = numberDictionary[#"Two"];
NSArray *temp = [dict allKeysForObject:#"012"];
NSString *key = [temp lastObject];
Note: allKeysForObject returns an NSArray, as more keys, may have the same value assigned

Resources