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

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?

Related

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.

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

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.

Is there an ObjC macro or function to get all the argument values received by a method? [duplicate]

This question already has answers here:
Pass all arguments of a method into NSLog
(2 answers)
Closed 8 years ago.
I know that NSStringFromSelector(_cmd) gives the name of the current method. I use that in a debugging macro to print what method I'm in. I would like to also get the method's arguments as strings so I can print them too. I'm thinking of something like *argv[] in C.
Is there any built-in facility for that?
Yes, c++ (and Objective-C) support va_list.
Do something like this:
- (void)do:(va_list)args
{
// do something with all your args
}
And use it like this:
[self do:#"foo", #"bar"]

What does `->` symbol represent in objective-c [duplicate]

This question already has answers here:
What does this ' ->' mean in c/objective-c?
(7 answers)
What is the difference between '->' (arrow operator) and '.' (dot operator) in Objective-C?
(3 answers)
Dot (".") operator and arrow ("->") operator use in C vs. Objective-C
(5 answers)
Closed 10 years ago.
I have been looking at some code and come across the symbol -> being used like obj->method(argument); I have done a little bit of research and found it basically is the same as [obj method:argument]; but I am unsure what -> actually is or does.
So my question is, what does the -> symbol mean in objective-c?
It means the same as the struct dereference operator does in C, which is used to access fields within the struct via a pointer:
struct mystruct
{
int field;
};
struct mystruct *mystruct = ...;
printf("field=%d\n", mystruct->field);
In Objective-C it can also be used to access fields within Objective-C objects:
#interface MyObj : NSObject
{
#public
int field;
}
#end
MyObj *myObj = [[MyObj alloc] init];
NSLog(#"field=%d", myObj->field);
Note that you can only access these fields externally if they are declared #public.
I have been looking at some code and come across the symbol -> being
used like obj->method(argument); I have done a little bit of research
and found it basically is the same as [obj method:argument]; but I am
unsure what -> actually is or does.
So my question is, what does the -> symbol mean in objective-c?
Exactly the same thing it means in C; it is for accessing an item in a C structure. Way back in the days of yore, Objective-C was implemented purely as a C preprocessor extension + a runtime. Classes were nothing more than concatenated C structures and the preprocessor turned each ivar access into self->ivar.
I.e. ivar and self->ivar do the same thing (in a method of class).
Now, you can use -> to poke at some other object's (#public) ivars. But don't. That breaks encapsulation exactly because Objective-C's line of encapsulation is drawn at the method interface. Always use the setters/getters such that behavior can be either observed or overridden.
Finally, no, there is nothing like obj->method(argument) anymore. There was, once, in a failed experiment called Modern Syntax, but it was abandoned because it was a pointless waste of time. You can't use -> to invoke methods.

What is the meaning of #[object1, object2]? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What kind of object does #[obj1, obj2] create?
Looking at the Master-Detail Template in Xcode, in the App Delegate the SplitViewController's view controllers are set like so:
self.splitViewController.viewControllers = #[masterNavigationController, detailNavigationController];
I don't know what the purpose of the # sign is before the square brackets. Is this just how NSArrays are made when not using [NSArray arrayWithObjects:]?
It's a new syntax feature. It's syntactic sugar for creating an array (NSArray) with the given objects.