As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Just a quick question - what is the difference (if any) between:
NSObject* myObj
and
NSObject *myObj
and even
NSObject * myObj
?
From a technical point of view, it doesn't matter.
Personally I use the last option (* in the middle), but it's a matter of personal taste.
A corner case would be the definition of multiple inline declarations, like
NSObject *a, b
as opposed to
NSObject *a, *b
The first case will produce a pointer to NSObject and a NSObject (which of course is forbidden and it wouldn't compile), whereas the second one will produce two pointers.
My understanding is this is totally a style issue, however, a team of developers may wish to match style and certain [open source] projects may have guidelines they enforce. For example, here's Github's Objective-C conventions: https://github.com/github/objective-c-conventions
(they use NSObject *myObj)
There is no difference. Both variations represent a pointer readable by Objective-C.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
The syntax is legal. It'll save you about 2 seconds every time you alloc/init an object. Do you think this could be a new trend?
Again,
NSObject *obj = NSObject.alloc.init;
[NSObject new];
Saves even more and doesn't cause any religious wars.
Yes, it saves a few keystrokes, but I doubt it will catch on. See this blog post for a number of cases where using dot notation on methods rather than properties will not work. I think most people view this as a side effect of ObjC2.
Furthermore, I don't think it enhances readability at all. As soon as you want to send a message with an argument, you have to revert back to the original syntax.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm in the process of learning Objective-C and I've read that I should be using both bracket notation exclusively and dot notation exclusively. Is there any practical benefit to using one over the other, or is it personal preference?
Dot notation is the current best way to access properties in Objective-C. The arguments for one paradigm or another are largely stylistic, and Apple's style is dot notation. As an Objective-C beginner, you should always strive to emulate Apple's style. Whoever told you to exclusively use bracket notation is incorrect.
And, though you didn't ask this I'm going to mention it here because it's a perpetual source of confusion for people just learning Obj-C. The following setters are identical
[self setFoo:42];
self.foo = 42;
the following getters are identical
bar = [self foo];
bar = self.foo;
You should always use dot notation whenever you're dealing with properties. Not only is it quicker to write (self.foo has less characters than [short foo]) but more importantly it makes chained messages easier to understand. For example:
self.myTextField.text.length
is easier to understand than
[[[self myTextField] text] length]
Additionally, it makes it less likely that you'll mess up the punctuation (Making sure that you have the correct number of brackets is oftentimes a pain).
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Imagine an ordinary UIViewController subclass. We want to do "setup stuff" to be preformed before -viewDidWhatever, but the problem is that there are three methods that could possible be called, either -initWithNibName, -initWithCoder or simply -init depending on how it is created.
The solution would be to create -setup to be called from all these three, but I'm a bit unsure about the name. My question to you is: is there any standard naming of this method?
I have commonly named it -setup myself when doing this.
Depending on what you want to do, you could put it in + (void)initialize, which is inherited from NSObject and will automatically be called before the class receives its first message.
Have a look here: Apple Developer Library: NSObject Class Reference
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Can I make POST or GET requests from an iphone application?
In this article, the second answer mentions 'Assume your class has a responseData instance variable, then.' What does this mean and how would I add one?
Thanks!
In your class's header file, there will be a section along these lines:
#interface YourClass : NSObject {
NSMutableData *responseData;
}
// Soem method declarations here
#end
Inside, the braces, add a declaration for the instance variable you want:
#interface YourClass : NSObject {
}
// Soem method declarations here
#end
I'd recommend reading The Objective-C Programming Language if this is unfamiliar to you, because there are a lot of aspects of the language that are more subtle than this.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
To better understand ARC I'm looking for example code that compiles perfectly with ARC enabled , but crashes at runtime. Ie the common pitfalls that may be overlooked and can cause you a debugging nightmare if you've never encountered that issue before.
Real life examples tuned down to the minimum code that reproduces the issues would be very helpful. In particular if the ARC code is interfacing with C or C++ code.
Quick example of the many I was thinking along the same lines as bbum.
Casting from CF... to NS... confuses ARC if done incorrectly for example:
CFArrayRef *supportedInterfaces = CNCopySupportedInterfaces();
NSArray *interfaceNames = (__bridge_transfer NSArray *)supportedInterfaces;
CFRelease(supportedInterfaces);
Would over-release supportedInterfaces since __bridge_transfer retains the NSArray while releasing the CFArrayRef. In this case either don't use the CFRelease() or use plain __bridge.