Objective C compiler not complaining about syntax error [duplicate] - objective-c

This question already has answers here:
Semicolon after the method name in Objective-C implementation file
(6 answers)
Closed 9 years ago.
I'm trying to figure out why gcc is OK with the following:
- (void) methodname: (id) sender;
{
// do stuff
}
Notice what's wrong here: There is a semicolon that should not be there.
I'm also finding that execution of the method is having bizarre results.
Anybody know what effect the semicolon is supposed to have or not have here?
Thanks.

The semi-colon is optional in the context of the #implementation. Some teams standardize on requiring it, some don't.

In general ; makes it a function prototype. But the compiler must be smart enough to ignore it in this case. I don't think it's the ; that's causing your odd issues but I'm not sure.

Related

Objective-C to Swift 3 Method Name Change Issue [duplicate]

This question already has an answer here:
Use objective-c method in swift seems crossing keyword of swift language
(1 answer)
Closed 6 years ago.
I am trying to use a method from Objective-C in Swift 3, but Swift 3 is translating the signature to something invalid for the compiler.
- (void)doWhenReady:(void(^)(void))block onDone:(ErrorCallback)callback;
Gets translated to
do(whenReady: ()->(), onDone: ErrorCallback)
Where do becomes the keyword do and invalid syntax for the method. What solutions do I have to fix this without refactoring the Objective-C code?
In the Swift expression do(whenReady..., write do with backtick characters around it.

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.

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

Bracket syntax in Objective-C [duplicate]

This question already has answers here:
What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?
(4 answers)
Closed 9 years ago.
I like to mess with coding every now and then as a hobby and I noticed an unfamiliar syntax in some of the Apple Developer documentation:
newSectionsArray[index]
I normally expect something like:
[object method]
Can anyone explain this to me?
Thanks!
It's called object subscripting, as explained here
Its syntactic sugar, as
newSectionsArray[index]
gets translated by the compiler to
[newSectionsArray objectAtIndexedSubscript:index];
NSDictionary implements subscripting too, so you can access an element in this fashion:
dictionary[#"key"]
The cool (and potentially dangerous) feature is that this is generalized, so you can even have it on your own classes.
You just need to implement a couple of methods
(for indexed access)
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
or (for keyed access)
- (id)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)obj forKeyedSubscript:(id)idx;
and you they will be called whenever you use bracket notation on the instances of you custom class.
So you could end up coding a grid-based game and accessing the elements on the grid by
board[#"B42"]; // => [board objectForKeyedSubscript:#"B42"]
or moving a piece on the board by
board[#"C42"] = #"Troll"; => [board setObject:#"Troll" forKeyedSubscript:#"C42"];
Nice, but I wouldn't abuse of it.
That's literal syntax, introduced in Clang 3.4. You could however use the old syntax [newSectionsArray objectAtIndex:index]. it's the same thing.
newSectionsArray is probably an array (i.e. a contigous block of multiple objects of the same type) and index an integer. newSectionsArray[index] gives you the object at position index (starting counting with 0).

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.