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.
Related
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?
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.
This question already has answers here:
Weird objective-c syntax - square brackets and # sign
(2 answers)
Closed 8 years ago.
I come from a C++ background and am learning Objective-C.
One expression that I've encountered is not clear for me. It is as follows:
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
What I don't understand is "#[indexPath]". Why do i need [] and #?
The method is...
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:...
and takes an array as the parameter (i.e. multiple index paths).
The code...
#[indexPath]
uses objective-c literals to create an array.
The equivalent in "old" code is...
[NSArray arrayWithObjects:indexPath, nil];
This question already has answers here:
Constants in Objective-C
(14 answers)
Closed 8 years ago.
I have an iOS app that requires me to have a "bank" of multiple strings. What I mean is that I need to have several strings that I can call upon at any time. Here is what I am thinking of.
// Strings.h
#define STR_ONE #"1"
#define STR_TWO #"2"
// ...
And when I need to use these strings, I simply include the header file. I chose to go with a header file because there will be many of these strings, and I just wanted to keep them separate.
So the question: Is this the best approach to solve my problem? Are there any alternate (and better) ways that I am missing?
Side notes: Is there any memory management I need to be thinking about here?
Should this be written to a file, and drawn upon from there?
Thankyou
NSArray: you can store a fixed amount of string insiden an array
NSArray* nameArr = [NSArray arrayWithObjects: #"Jill Valentine", #"Peter Griffin", #"Meg Griffin"
NSMutableArray: this type of array can expand and decrease in size.
NSMutableArray *names = [[NSMutableArray alloc] init];
[self.names addObject:#"Harry Potter"];
If the amount of Strings is not enorm, a simple Plist will work for you. But i also would recommend you to read about core data.
Property List Link
This question already has answers here:
What is the Java equivalent of Objective-C's NSDictionary?
(3 answers)
Closed 9 years ago.
I am interested in creating the java equivalent of a Map data structure that looks as follows:
Map <Object ---> NSMutableArray of objects>
or
Map<Object,Array<Object>>
Can anyone provide guidance on what would be the best way of doing this in objective c as I am fairly new to the language.
Objective-c does not have typed collections. You just create NSMutableDictionary instance, and put NSMutableArray into the values.