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.
Related
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.
This question already has answers here:
What's the difference between dot syntax and square bracket syntax?
(5 answers)
Closed 9 years ago.
I want to set a property but I'm not sure what is the best way to do it. I can think of two ways to do this. By creating an object, storing it in a variable, and setting the property:
PropertyClass *myProperty = [[PropertyClass alloc] init];
self.myClassProperty = myProperty
Or by creating and setting in one line:
self.myClassProperty = [[PropertyClass alloc] init];
Are there any reasons one way is actually better/safer/faster/etc.? Is there a better way than the two I listed above? I'm not sure if this makes a difference but this would be the first time the property is set.
They are equal but I always use the second one. Or the following
self.myClassProperty = [PropertyClass new];
The reason - in one line of code it is more difficult to make some stupid error. Also it is easier to read.
This question already has answers here:
Setting a cell's text with multiple JSON objects
(3 answers)
Closed 9 years ago.
Real beginners question here. I'm doing some tutorials and I'm expanding on them with scenarios that might come in to play.
Anyhow, I know that the following works:
lblCopyStatus.text = #"Test";
But how would do:
lblCopyStatus.text = #"Test" & lbltest.text;
e.g. get the text from another label into the text. I hope that makes sense :)
For example:
lblCopyStatus.text = [NSString stringWithFormat:#"Test %#", lbltest.text];
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Asterisk in parenthesis in Objective-C… What does it mean?
does anyone can find some resource or detail explanation about following code
-(void) add:(Cal *)c;
I am very new to obj-c.what does asterisk means in that code?
Thanks.
It means that the argument for the method is a pointer to an instance of a "Cal" object.
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.