Objective C expression not understood [duplicate] - objective-c

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];

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.

Objective C syntax, iOS sample code [duplicate]

This question already has answers here:
What does a type name in parentheses before a variable mean?
(3 answers)
Closed 8 years ago.
I'm learning objective-C and I was looking at some sample code from:
https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Listings/SamplePhotosApp_AAPLAssetGridViewController_m.html#//apple_ref/doc/uid/TP40014575-SamplePhotosApp_AAPLAssetGridViewController_m-DontLinkElementID_8
I'm confused about this line of code here:
CGSize cellSize = ((UICollectionViewFlowLayout *)self.collectionViewLayout).itemSize;
I understand that it's trying to get the itemSize property and store it into cellSize, but I have no idea what ((UICollectionViewFlowLayout *)self.collectionViewLayout) is all about. Can someone break it down for me? Is there another way to write this line of code?
What it means is:
Cast self.collectionViewLayout to be of UICollectionViewFlowLayout type. Then of self.collectionViewLayout get the itemSize property. Finally, save everything is a property of type CGSize.
I believe is a elegant and concise way of writing it.

what does the caret sign mean in Objective-C? [duplicate]

This question already has answers here:
Caret in objective C
(3 answers)
Closed 9 years ago.
There is a piece of code like
typedef void (^SignIn) (NSString *email, NSString *password);
What does the ^ mean before SignIn? Is this Objective-C specific usage?
It's the syntax for blocks.
That typedef declares SignIn to mean a block which takes two NSString* arguments and returns void (i.e. nothing).
It is a block.
For a guide to understanding blocks, see this tutorial
Unless, you already know what a block is, and you just didn't know what the caret was for.

UILabel.text == self.NSString? [duplicate]

This question already has answers here:
Compare two NSStrings
(3 answers)
Closed 9 years ago.
My question may be simple for some to answer, but I'm relatively new to Objective C and Xcode. So I have a UILabel and I am running an if statement asking if UILabel is equal to self.NSString then do ... Here is the code.
if (UILabel.text == self.NSString)
{
//Do Something here...
}
I'm wondering if this would work, or what I have to do in order for this to start working.
Thanks in advance.
Use isEqualToString: method from NSString class.
if([text isEqualToString:string])
{
// Do something here.
}
When comparing strings, you should use
[UILabel.text isEqualToString:self.NSString]
The == simply compares the pointers, which will often be different even if their contents are the same. The isEqualToString method compares the contents.

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.