How to find the size of a structure.The use of sizeof() doesnt work in objective C.
sizeof does work for struct in Objective-C. For example:
size_t pointsize = sizeof(NSPoint);
On the other hand, if you are interested in the size of Objective-C instances, use class_getInstanceSize(). For example:
#include <objc/runtime.h>
size_t objsize = class_getInstanceSize([NSObject class]);
Related
I am trying to write this without dot notation but can't figure it out:
[[textField text] rangeOfCharacterFromSet:someInstanceVar].location
I keep getting bad receiver type NSRange (aka '_struct NSRange')
Is this not possible?
Kind regards
You cannot call that without the dot.
rangeOfCharacterFromSet returns a NSRange, which is a plain C struct:
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
and not an Objective-C object. .location accesses the first member of that struct
and is pure C syntax.
That has nothing to do with the dot-notation for properties, or with method calls.
In C/Obj-C, we do a typedef like this typedef int MYINT; which is clear.
Doing typedef for a block -typedef void (^MyBlock) (int a);
Now, we can use MyBlock.
Shouldn't it be like - typedef void (^MyBlock) (int a) MyBlock; similar to #define?
How the syntax works?
See Declaring a Block Reference in "Blocks Programming Topics":
Block variables hold references to blocks. You declare them using
syntax similar to that you use to declare a pointer to a function,
except that you use ^ instead of *.
So
typedef void (^myBlock) (int a);
defines a the type of a block using the same syntax as
typedef void (*myFunc) (int a);
declares a function pointer.
See e.g. Understanding typedefs for function pointers in C for more information about function pointers.
Also from "Blocks Programming Topics", creating a type for blocks should be like this:
typedef returnType (^blockName)(argument1, argument2, ...)
Below is a very simple practical example:
typedef float (^MyBlockType)(float, float);
MyBlockType AddTwoFloat = ^(float a, float b) {return a + b;};
MyBlockType MultiplyTwoFloat = ^(float a, float b) {return a * b;};
float c = AddTwoFloat(1, 2); //c = 3
float d = MultiplyTwoFloat(1, 2); //d = 2
I'm learning the basics of objective-C by Reading 'Objective C For Dummies'.
I'm using XCode 4.4, and I'm trying to get some simple code to work. This question has been posed online before. However - the code doesn't seem to compile with the new version of XCode.
At issue seems to be the line NSLog (#"Here is some amazing text! %i",c); This throws an 'Expected Expression' Error. Per the previous form posting, I have disabled automatic reference checking in preferences and this still fails.
#include <stdio.h>
int main(int argc, const char * argv[])
{
//declare variables
int a;
int b;
int c;
//set the variables
a = 2;
b = 3;
//Perform the computations
c = a % b;
//Output the results
NSLog (#"Here is some amazing text! %c",c);
return 0;
}
Add #import <Foundation/Foundation.h> at the top, and change the NSLog to this:
NSLog (#"Here is some amazing text! %d",c);
Because %c doesn't mean "a variable called c", but rather a char. %d means an int, which is what c is.
You forgot to include the Foundation header:
#import <Foundation/Foundation.h>
Sidenote: The format specifier should be %d.
I was looking at enums in cocoa frameworks and I saw this:
enum {
NSNetServiceNoAutoRename = 1UL << 0
};
typedef NSUInteger NSNetServiceOptions;
and my question is how is this possible?
How is NSNetServiceOptions tied to that enum?
And is it only possible in objective c or also in c?
NSNetServiceOptions tied to that enum in the context that the enum is going to hold an integer value anyway. In the above example you will create a variable for the enum as,
NSNetServiceOptions _netServiceOptions;
You can even ignore the typedef and directly use,
NSUIInteger _netServiceOptions;
enums in C (and consequently Obj-C and C++) are weakly typed, which means you can implicitly casts between enums and ints however you like as they are just ints.
For example, this is perfectly valid:
enum {A = 1};
enum {B = A+1};
const int C = A | B;
The reason the the enum uses a typedef instead of the shortform typedef enum {...} Name; is because enums defaults to being of type int. By using a typedef you can define the enum as being an unsigned integer instead.
I'm making the jump from Java to Objective-C. I'm wondering if there is a concept analogous to Java's enums, that supports implementations of methods. I realize Objective-C has plain old C enumerations, but those are really just ints.
I'm looking to prevent switch-yards -- Java enums would be perfect.
Objective-C is just C with some additional markup for objects, no new types have been added.
That means, no.
For mutual exclusive flags, Apple uses strings.
header.h
extern NSString * const kNSSomeFlag;
extern NSString * const kNSOtherFlag;
extern NSString * const kNSThirdFlag ;
code.m
NSString * const kNSSomeFlag = #"kNSSomeFlag";
NSString * const kNSOtherFlag = #"kNSOtherFlag";
NSString * const kNSThirdFlag = #"kNSThirdFlag";
…
void myFunction(NSString *flag)
{
if (flag == kNSSomeFlag) {
// the code
}
}
An example of this can be found in the NSDistributedNotificationCenter.