Why does NSDictionary uses = sign inside json objects? [duplicate] - objective-c

This question already has answers here:
Generate JSON string from NSDictionary in iOS
(14 answers)
Closed 8 years ago.
I am creating a new JSON (NSDictionary) object in Objective-C using various methods such
objectForKey
but when I actually print the NSDictionary object after converting it to NSString, I see that unlike the colon (:) separator, it uses the equals (=) separator between the key and value.
How can I ensure that the NSDictionary uses the : as a separator between the key and value?

It is just a formatting issue. If you create an NSJSON obkect from it, that object will be in correctly formatter JSON. When you log NSDictionary to console or otherwise print it, it uses some custom formatting, probably unrelater to the actual internal structure.

Related

Creating an NSDictionary by writing values inside curly braces [duplicate]

This question already has answers here:
Can the new Clang Objective-C literals be redirected to custom classes?
(2 answers)
Objective-C at sign and curly braces, #{ ... } what does it mean?
(3 answers)
Closed 5 years ago.
The NSDictionary class allows me to initialise it like this:
NSDictionary* attributes =
#{
NSFontAttributeName: font
};
What is this construction method called, and how I can implement it in my own class? I want to have a class with properties that have default values, and I'd like to only set the ones I need during construction (in C++ I would have done this with default parameter values, but Objective-C does not have that), so this seemed like a legitimate approach. Or is this something NSDictionary specific?

Create Objective-C literal object like NSString [duplicate]

This question already has answers here:
Can the new Clang Objective-C literals be redirected to custom classes?
(2 answers)
Closed 6 years ago.
I want to know if it's possible to create Objective-C literals like NSString, where instead of [[Object alloc] init], and then assigning you can just assign a value to it, such as #"A string".
Obviously NSString is an object because it has methods to manipulate the data in addition, so in theory there should be a way to do it yourself, but I'm not sure where to even go about finding stuff like this.
Objective-C is C. The primitive (what I would call scalar) data types are all numbers and are completely defined by the language; you cannot add to them (though you can rename them using typedef. The corresponding literals, such as 1 and "hello", are also part of C.
Similarly, literals like #"howdy" and #[#"howdy"], though defined by Objective-C rather than C, are part of the language and you cannot change or add to them, as the literal syntax is built into the language.

Problems adding key/values to a NSMutableDictionary [duplicate]

This question already has answers here:
Editing an NSMutableDictionary sub-dictionary
(2 answers)
Closed 8 years ago.
Fairly new to this. I am trying to add some value/keys to an array that already has value/keys.
The itemsArray contains the json and is derived from the variable json which is a parameter passed to the method from a rest api call.
This is the code I am using and it aborts (at [i setobject] with [__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
NSMutableDictionary *itemsArray = [json objectForKey:#"items"];
for ( NSMutableDictionary *i in itemsArray) {
[i setObject:#"FirstValue" forKey:#"parents"];
}
All this should do is add in the key called parents for each item in the dictionary.
Now if I hard code the value/keys to the mutable dictionary itemsArray - it works fine. Its just when I use the variable json which is passed as a parameter and obtained from a call to a rest api. So the issue is likely that itemsArray contains a immutable json object even though itemsArray is mutable.
The question I have is how do I make the json object mutable (assuming that will cure it) so it will work. ?
You have the answer right in the error message: itemsArray is a NSDictionary not an NSMutableDictionary. See the answer to What is an NSCFDictionary? for more details.
How did you create json? You need to make sure it contains an NSMutableDictionary for the key #"items".
You will need to create a mutable dictionary from your itemsArray. Something like:
NSMutableDictionary *mutableDictionary = [i mutableCopy];
You can then add objects to it as you'd like.

How to Convert a Objective C class into JSON object? [duplicate]

This question already has answers here:
Serialize and Deserialize Objective-C objects into JSON
(7 answers)
Closed 9 years ago.
I have a class with data as follows
Class1 *obj = [[Class1 alloc] init];
obj.One=#"1";
obj.Two=#"2";
obj.Three=#"3";
I want to convert this object into
{"One":"1","Two":"2","Three":"3"}
How Can I do that in Objective C ?
You will have to write an instance method for your class to serialize the data (ie: convert the member variables into valid JSON data) and you may want another method to parse JSON data back into the class.
If you're wondering if there is a standard method somewhere to do this for you, the short answer is no -- Since class member variables can be of any object type, they must first be converted to valid JSON data types. (String, number, boolean, array or dictionary)
In your example, this would entail creating a dictionary like this:
NSDictionary *dictionary = #{#"One" : obj.One, #"Two" : obj.Two, #"Three" : obj.Three};
Then you will need to use the NSJSONSerialization class to convert the NSDictionary into a valid JSON string.

Format String is not a literal string (potentially insecure) warning [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: “format not a string literal and no format arguments”
I have the following line of code that is in my app that a Developer worked on. I am learning the basics of Objective C and as I was updating the App to be iPhone 5 compatible, I see the following warning (I did not change his code) Format String is not a literal string (potentially insecure). The code is as follows;
self.progressHud.labelText = [NSString stringWithFormat:message];
I don't know exactly what this means and don't want to upload anything that can either be a security issue or get rejected by Apple. Any and all help is appreciated from you all.
Use the following Lines:
self.progressHud.labelText = [NSString stringWithFormat:#"%#", message];
In objective C, this line get the values from any format such as int,float etc to display the Label. because UILabel and IBOutlet Elements only display the NSString values.
However, if you don't need to create a string with multiple variables, it would be more efficient to simply use:
self.progressHud.labelText = message;