Objective C Struct Syntax [duplicate] - objective-c

This question already has answers here:
What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?
(3 answers)
Closed 7 years ago.
I want to understand the syntax of the struct which i have seen some where. Can some please explain the meaning of unsigned int xyz:1;. Is it just assigning default value to a variable xyz? BTW this code is in Objective C.
struct
{
unsigned int xyz:1;
} testStruct;

It's a bit field. You are telling the structure that you will only be using one bit of xyz.
This allows the compiler to make packing optimisations.

Related

Why would we use a pointer in an NSInteger but not in an NSNumber? [duplicate]

This question already has answers here:
What's the difference between NSNumber and NSInteger?
(5 answers)
Closed 6 years ago.
I have an NSInteger that doesn't use a pointer and an NSNumber that does use a pointer. Can someone explain to me why this is the case? All my teacher said was the NSInteger is being used as a type alias but I'm not familiar with that either yet.
This question asked a direct question asking for the reason there was no pointer in NSInteger; not asking for all the differences.
Bear in mind that Objective-C is C. Thus:
An NSInteger is a scalar, a built-in C data type (an integer). [The actual size of this integer depends on the architecture, 32-bit vs. 64-bit. But it is still some form of C integer.]
An NSNumber is an object; Objective-C object references are represented as C pointers.

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.

What's the equivalent of Math.abs() in Objective-C? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert to absolute value in Objective-C
As i am playing with 2d graphics, i'd like to calculate amount of points object has moved between 2 CGPoints. Given that object can move in both direction, i am only interested in sheer number of points representing the difference.
In Java i'd Math.abs(startpoint.x - endpoint.x)
How can i do the same in Objective-C?
There are C functions from <math.h> that will do what you want:
abs(int val);
labs(long val);
llabs(long long val);
fabs(double val);
fabsf(float val);
fabsl(long double val):
Given that CGPoint structures are composed of CGFloats, you should use fabsf here.
Check out the Wikipedia page

Objective C: NSString* myVar; vs. NSString *myVar; [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Placement of the asterisk in Objective-C
I am confused, is there a difference between declaring a pointer as NSString* myVar; and NSString *myVar; ?
i.e. is the location of the asterix significant?
No, there is no difference. However I think that 2nd one is more readable.
int* a, b;
int *a, b;
In the 2nd one it is clear that a is a pointer but b is not. But in the 1st line it looks like both a and b are pointers which is not true.
But still, this is a personal choice and there is NO difference from compiler's point of view in the two lines.
No.