Is there a modern Obj-C short notation like #[array] or #{#"dict":dict} for CGPoint or NSPoint? - objective-c

This is great:
NSArray *myArray = #[0, M_PI];
NSDictionary *myDict = #{#"item":anItem, #"another":somethingElse};
//etc.
But this is tedious:
[baseline moveToPoint:NSPointFromCGPoint(CGPointMake(0, baselinePosition))];
[baseline lineToPoint:NSPointFromCGPoint(CGPointMake(self.bounds.size.width, baselinePosition))];
Is there anything like #(0, 0) to simplify writing a point literal in code?

C99 compound literals to the rescue:
(CGPoint){ 0, 0 }
or
(NSPoint){ 0, 0 }

As H2CO3 points out, you can just cast a literal - this is plain old C, and NSPoints and CGPoints are just C structs. nothing fancy here.
As a consequence, there is no Objective-C shortcut, modern or otherwise, but there is an old macro which will save you a few keystrokes compared to the sequence you came up with:
NSMakePoint(self.bounds.size.width, baselinePosition)
You'll notice that the old macros and various constants used in the Cocoa API follow a natural English word order:
NSMakeRect(), NSMakeRange(), etc.
and for the options (e.g., in NSString)
NSAnchoredSearch, NSNumericSearch, NSCaseInsensitiveSearch, etc.
The concept that these options have in common ("search") is added to the end of the symbol name, as was the convention when the NSString class was designed.
The more modern APIs use a naming convention so names that belong to the same "group" begin with the same word(s). This makes it easier to alphabetize and greatly helps the autocomplete suggest the possible options for a parameter value. You can see this in NSOperation, for instance.

Related

Difference between (25) and #(25)

I have been brushing up on my objective-c and one of the exercises that I am working on deals with UIInterpolatingMotionEffect objects. 2 properties from that class are minimumRelativeValue and maximumRelativeValue. In the exercise they have them as the following:
motionEffect.minimumRelativeValue = #(-25);
motionEffect.maximumRelativeValue = #(25);
I know that in obj-c you use # for the following (from wikipedia):
Used to avoid taking english words and making them reserved (for example, you can't have a variable called float in C/Objective-C because this is a reserved word).
With that being said, what would be the difference between using #(-25) and (-25) in the above example?
25 is a number. #(25) is an NSNumber object with value 25. It is equal to
[NSNumber numberWithInt:25];
In some cases you have to wrap numbers with NSNumber. For example you can not add number to NSArray / NSMutableArray.
You may read more about Objective C literals here https://www.mikeash.com/pyblog/friday-qa-2012-06-22-objective-c-literals.html
(-25) is just -25 with brackets around. You can add backets to lots of numerical or logical expessions without (or sometimes with) changing its value.
The # is one of the object creation shortcuts in objective-c. In this case it is equivalent to #-25 (Frankly, I am not sure whether there needs to be the brackets here because of the minus) which is equivalent to [NSNumber numberWithInt:-25].
See more of them here: http://clang.llvm.org/docs/ObjectiveCLiterals.html

What does #() mean in Objective-C?

For example,
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
[rotate setToValue:#(M_PI)];
[rotate setDuration:0.1f];
[[aView layer] addAnimation:rotate forKey:#"myRotationAnimation"];
where M_PI is defined as a macro in math.h,
#define M_PI 3.14159265358979323846264338327950288 /* pi */
It's a pointer to an NSNumber object. It's called a boxed literal, because the mental picture is of putting a primitive value of expression inside into a "box", that is, an object.
See official documentation if in doubt. Note that pointer can be to a "real" NSNumber object or it can (theoretically, don't know whether this will work in practice) be a tagged pointer (see, e.g., my question).
Note that you can also do things like #"string" and #5, which will create constants in compile time. But you need parentheses to use something which is not a literal, e.g. #(2 + 3). Parentheses form can be used for any expression, even those that compiler cannot compute at compile-time (although if it can, it will just put an expression result into code).
NeXT and Apple Obj-C runtimes have long included a short-form way to create new strings, using the literal syntax #"a new string". Using this format saves the programmer from having to use the longer initWithString or similar methods when doing certain operations.
When using Apple LLVM compiler 4.0 or later, arrays, dictionaries, and numbers (NSArray, NSDictionary, NSNumber classes) can also be created using literal syntax instead of methods. Literal syntax uses the # symbol combined with [], {}, (), to create the classes mentioned above, respectively.
So, basically it's not only for id or NSNumber object!
thanks to wiki.
It's Shorthand writing
In Objective-C, any character, numeric or boolean literal prefixed with the '#' character will evaluate to a pointer to an NSNumber object (In this case), initialized with that value. C’s type suffixes may be used to control the size of numeric literals.
'#' is used a lot in the objective-C world. It is mostly used to avoid taking english words and making them reserved (for example, you can't have a variable called float in C/Objective-C because this is a reserved word).
Use this link To have detailed knowledge of '#' symbol.
In Modern Objective C, '#' symbol is used extensively.
What You can do with it:
calculate an expression: #(<Expression>)
wrap any value like int,bool,float,char in same way
Reasons to use:
Easy to write, Less code required
Less chances of mistakes. Compare [NSNumber numberWithInt:3] with #3.
Get rid of typecasting issues in simple cases.
It represent id Object
that you can use any expression in it or return any object.
Syntax : #(<#expression#>) it will return id object.
So in your case it will returning NSNumber object to setToValue method.

Is there an Objective-C equivalent to C#'s 'var' keyword?

Huge proponent of using the 'var' keyword in C# for cases where it's very clear. For instance, rather than this...
ThisIsMyReallyLongFooClassName foo = new ThisIsMyReallyLongFooClassName();
I can type this...
var foo = new ThisIsMyReallyLongFooClassName();
...and I still have a strongly-typed variable. The two are, for all intents and purposes, equal. The latter is just more readable (again, because it's clear. There are cases where it isn't and 'var' shouldn't be used. I don't want this to become a discussion of that however.)
I'm wondering if Objective-C has anything similar.
Yes and no.
You can use id foo = ... which will always work, but you lose the type information.
If you really want something equivalent, you could use auto foo = ... from C++11, but then you have to compile your file as Objective-C++, which has many other side effects.
Convention is to just spell out your types; it's annoying, but unlike C++, C#, Java where templates/generics can make typenames very long, it's usually manageable in Objective-C.
There is now, __auto_type. For example...
__auto_type test = #"Hello World";
...results in test having the type NSString*.
Here's a decent writeup:
https://medium.com/#maicki/type-inference-with-auto-type-55a38ef56372
The author suggests using
#define let __auto_type const
#define var __auto_type
in some shared header in your application to make the usage cleaner. I'm a bit wary of this kind of macro usage personally but I've been doing it for a while and the world is still turning... Maybe macro names less likely to cause a collision would be better.
No, there is no equivalent in Objective C. C++11 introduced the auto keyword to do it, but neither C nor Objective C has a similar capability.
The id is closest to C#'s dynamic keyword. It lets you achieve similar results to var, except that it does not let you access properties using the property syntax. It does let you invoke methods, including methods that implement property accessors.
You can do something like this:
__typeof([obj someMethod]) foo = [obj someMethod];
That's ugly, but if you have a snippet or macro defined to automatically generate it, then you don't have to type out the type names. For example:
#define LET(V, EXPR) __typeof(EXPR) V = (EXPR)
LET(vc, self.viewController); // equivalent to "UIViewController* vc = self.viewController;"
LET(d, [number doubleValue]); // equivalent to "double d = [number doubleValue];"
LET(foo, [[Foo alloc] init]); // equivalent to "Foo *foo = [[Foo alloc] init];"
Note: I'm not recommending this approach, as the convention in Objective-C is to write out the full type names or use id, and macros can be messy. But knowing about __typeof() can be handy.
There is the id keyword in Objective-C, but note that it is equivalent to the dynamic keyword in C# and not the var keyword. var is implicit typing - ie the type is inferred, but it is still static typing. dynamic and id are for dynamic typing and you lose type information.
I am afraid that no such equivalent exists in Objective C which would allow you to preserve strong typing.
You can use id keyword in objective C, but it doesn't work as c#
in c#
var stringVar = ...
stringVar worked as string variable and you can use access the string function by doing stringVar.function
id stringVar = [NSString ...]
but it still work as normal id type.

New Objective-c Literals in 4.4

I can write #42, which creates an NSNumber with int value 42. Can I do this with a variable, like #someIntVar? Obviously I tried it and it doesn't work (which sucks because then I have to go through [NSNumber numberWithInt:someIntVar]). Is it possible with a slightly different syntax?
I strongly suggest you read the official clang documentation on the matter: http://clang.llvm.org/docs/ObjectiveCLiterals.html
But, to box a variable, or any expression, you can use parentheses:
id num = #(someIntVar);

Dot Notation vs Method Notation

I'm diving into iOS programming and I'm having difficulty getting my head around the idea of Dot Notation and Method Notation.
As far as I understand it, Dot Notation can be used to invoke setters/getters on properties and is much more cleaner to write/read. Method Notation is used to send messages to objects to manipulate them etc.
Could someone give me a simple explanation as to why the following two statements are essentially different and one will compile but the other will instead fail due to a syntax error.
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [sender currentTitle];
self.display.text = [self.display.text stringByAppendingFormat:digit];
self.display.text = self.display.text.stringByAppendingFormat:digit;
}
Thanks.
You're entering into Objective-C development at an interesting time where old syntax is being used with new syntax. Dot syntax is syntactic sugar and there are some cases where you can use it but you should not.
The following is invalid syntax. Anything where you'd use a colon (besides setters or getters), you won't use dot notation.
self.display.text = self.display.text.stringByAppendingFormat:digit;
Also, you would use stringByAppendingString, not stringByAppendingFormat
You use dot notation for accessing variables, not for calling actions that will have effects.
Correct:
self.foo.attributeOfMyClass
Incorrect:
self.foo.downloadSomethingFromAWebsite
Ensuring you always use dot notation for accessing property values and you always use bracket notation (even when you don't have to) for calling action methods, your code will be much clearer upon a glance.
Dot notation is just shorthand for a specific kind of method--namely, accessors. You may use it in the following cases:
When setting a property: foo.bar = 3; is equivalent to [foo setBar:3];.
When requesting a property: in any case except the one above, foo.bar is equivalent to [foo bar].
Dot notation is only shorthand--there is nothing magic about its relationship to properties. You could theoretically use dot notation to send any message that takes no arguments (foo.doSomething), but this would be very very bad style, as dot notation is intended for properties. Also note that if dot notation vs. square brackets is confusing you while you're learning, it's a perfectly valid choice to avoid dot notation altogether. It's just one shortcut you may use for accessors, if you like.
Actually, your second statement is not correct. Objective C way to invoke methods (messages) is using the [instance message] syntax.
As you said, the dot notation is just to call getters and setters on class properties, but not messages, that's why your second statement is not correct. The two lines you may wanted to compare are:
self.display.text = [self.display.text stringByAppendingFormat:digit];
[[self display] setText:[[[self display] text] stringByAppendingFormat:digit]];
Note that the message stringByAppendingFormat has to be called the normal way.
The dot notation is just to write faster and not so many brackets, but it will execute exactly the same instructions once compiled.
Another reason for using selector notation rather than dot notation is due to the dynamic language features in Objective C. As an example, consider the following:
NSString *s = #"Hello World!";
NSLog(#"Length is %d", s.length);
This works as we would expect. However, objects in Objective C may be passed around with type id. Consider the following:
id s = #"Hello World!";
NSLog(#"Length is %d", s.length);
This won't compile, as id doesn't have a property called length. The following will work, however:
id s = #"Hello World!";
NSLog(#"Length is %d", [s length]);
The reason this works is that Objective C knows about NSString, and so knows that there is some object type that responds to the selector length. Of course, if you try the following:
id s = [[UIView alloc] init];
NSLog(#"Length is %d", [s length]);
Your code will compile correctly, but a runtime exception will occur (unrecognized selector sent to instance) as UIView does not have a length selector.
Let's say we have the class Class with the variable variableOne we are going to use both notations.
Dot notation is the purest way to access a variable. It is also the way that bracket notation is most likely doing it behind the scenes. By typing Class.variableOne... variableOne is a part of Class and the "." after the class tells the compiler that it would like to access a part of the class--either a variable or a method.
Bracket notation is uses a method to access the variable. Let's say...
-(int) setVariable:x {
self.variableOne = x;
}
-(int) showVariable {
return self.variableOne
}
So when you're using bracket notation to set the variable [variableOne setVariable:5] or displaying the variable [variableOne showVariable] it calls the appropriate method.
This is a very simple way to think of the difference, I realize another answer has already been accepted but perhaps this answer will explain it for someone who didn't understand another answer.
When your code gets compiled, clang actually first takes all of your dot notation and turns it into method/bracket notation, so self.display and [self display] are exactly the same. Dot notation is actually fairly new as of Objective-C 2.0. It's simply for convenience.
Dot notation can only be used for properties, because doing something like you tried to do (which will not compile) gets cumbersome:
self.display.text.stringByAppendingFormat:digit;
It also wouldn't work for methods that take multiple arguments since you'd need to put spaces between arguments and suddenly the line of code would look awkward and hard to read.