If im declaring a class with property for example:
#property float radius, diameter;
So any object that will be created in this class or inherits from it, will have its own instance of radius and diameter right?
And this is also mean that they have their own setter and getter too right?
What i'm trying to fully understand is how do I deal with variable access in 2 ways:
1.When I want to make sure the user is not messing with the original value.
2.When I want to limit the users for instance variables he can get access and ones that he can not.
tnx
As to the first part, yes, any subclass will inherit it's superclasses properties and methods as long as they are public. Just remember to synthesise them to get the getter and setter.
So, if you want to have a public property that can't be modified:
// This returns a copy - whatever the receiver does with it doesn't affect the original
#property (nonatomic, copy) BOOL someVariable;
// This declares the property as only having a getter - no setter
#property (nonatomic, readonly) NSArray *someArray;
Any properties declared in your header file are considered public, unless otherwise specified. If you want to have private properties, ivars or methods, declare them in your implementation file in the class continuation category:
// In your .m *above* the #implementation MyClass
#interface MyClass()
#property (nonatomic, strong) NSArray *myPrivateModel;
#end
Stamford do a great lecture series on iTunesU which I'd really recommend for learning objectiveC
Hope that helps
Related
What is difference of these three cases (all of them are used like private fields):
1.
#interface APLParseOperation : NSOperation
#property (copy, readonly) NSData *earthquakeData;
#end
2.
#interface APLParseOperation () <NSXMLParserDelegate>
#property (nonatomic) APLEarthquake *currentEarthquakeObject;
#property (nonatomic) NSMutableArray *currentParseBatch;
#property (nonatomic) NSMutableString *currentParsedCharacterData;
#end
3.
#implementation APLParseOperation
{
NSDateFormatter *_dateFormatter;
BOOL _accumulatingParsedCharacterData;
BOOL _didAbortParsing;
NSUInteger _parsedEarthquakesCounter;
}
It's a good practice or smthing else?
I'm going to go through each example you gave, and describe them. I was just having trouble with this yesterday so I feel your pain.
1.
#interface APLParseOperation : NSOperation
#property (copy, readonly) NSData *earthquakeData;
#end
By using the #property keyword, the compiler automatically synthesizes your accessor methods for you, and also a backing instance variable. However, because you are using the readonly property attribute, the compiler is only synthesizing a getter method for you.
2.
#interface APLParseOperation () <NSXMLParserDelegate>
#property (nonatomic) APLEarthquake *currentEarthquakeObject;
#property (nonatomic) NSMutableArray *currentParseBatch;
#property (nonatomic) NSMutableString *currentParsedCharacterData;
#end
This second example is very similar to the first. However, because none of them have the readonly property attribute, they will all have getters and setter methods synthesized for them, as well as the backing instance variable.
3.
#implementation APLParseOperation
{
NSDateFormatter *_dateFormatter;
BOOL _accumulatingParsedCharacterData;
BOOL _didAbortParsing;
NSUInteger _parsedEarthquakesCounter;
}
For this last example, you are just declaring instance variables. These are also private to your implementation file, where as the other 2 examples had declarations being made in your classes interface file.
No setter or getter methods are being synthesized for you by the compiler. You are simply declaring some instance variables.
In terms of private and public, your first and second examples both provide declarations that will be visible to other classes, as long as they import the current class's header file. The first example however, only provides a way to "get" the property and read it, there is no setter method because you used the readonly property attribute. With the second example, outside classes will be able to access your getter and setter methods for your property, so they can read and write.
For the third example, these are just instance variables and they are private to your class's implementation file. Basically, no outside classes will even know that they exist.
This is not private. It is still readable by outside classes, though it can't be written.
Private properties. It can be useful if you want to write custom getters and setters. If you are not using ARC, it can be helpful for memory management.
Private members. This is my favorite. It's easy to read and easy to write.
defines a public property visible to all users of the APLParseOperation class.
defines properties through an extension, making them available only to the implementation methods.
defines instance variables which are implicitly private.
Number 1 is used when you want to make your properties public. Numbers 2 and 3 are for private properties and instance variables. You can also declare instance variables in class extensions, like this:
#interface APLParseOperation () <NSXMLParserDelegate>
{
NSDateFormatter *_dateFormatter;
BOOL _accumulatingParsedCharacterData;
BOOL _didAbortParsing;
NSUInteger _parsedEarthquakesCounter;
}
#end
There is not much difference between that and the number 3. It is a good idea to pick one style, and stick to it in all your code.
Case 1. is not private. It's a public read-only property: Reading is public, writing is only possible only in the private scope via the underlying ivar (thanks for pointing it out #mah).
Case 2. (if in a .m file) is extending the class by adding 3 private properties and making the protocol conformance private too.
Case 3. is declaring 4 private instance variables that can be used in all the implementation scope.
Objective-C best practice for private properties is case 2., as case 1. is not private at all, just read-only, and case 3. uses instance variables (aka ivar) which is less conventional than properties. More on that here: Reason to use ivars vs properties in objective c
Hope this helps,
In Objective-C, should a wrapper property be set to weak? By wrapper property I mean that the property is a wrapper of another property.
e.g.
- (Prop *)prop {
return self.obj.prop; //wrapper method for another property
}
#property (nonatomic, readonly, weak) Prop *prop; // weak or strong?
"correct" in this case is a matter of convention and preference as Objective-C properties do support this pattern. Accessing a property does call a method which is either generated, synthesized or manually coded.
I'll stay out of whether it's "correct" or not.
Since your property only returns the value of a different property, then you don't need to specify strong or weak as they are meaningless.
In the public header:
# myclass.h
#interface MyClass
#property (nonatomic, readonly) OtherClass *someProperty;
#end
In the implementation:
# myclass.m
#implementation MyClass
- (OtherClass *)someProperty
{
return self.someObject.otherProperty;
}
Using a property is not necessarily the best way to go. It shouldn't really be weak or strong as it will never be set. You are never going to create a readwrite counterpart and generate an instance variable / accessor methods.
So, it's better to represent this as a simple method in the header file. It's more accurate as a description of the interface offered.
What the difference between declaring an #property in .h or .m file
#property (nonatomic, readwrite, assign) BOOL notificationDidLaunch;
Is it to do with the scope of the variable?
Also in the .h file whats the difference between declaring a string with the brakets like so
#interface AppDelegate : NSObject < UIApplicationDelegate > {
NSString *hat;
}
and doing it outside of them like below
#property (nonatomic, strong) NSString *hat;
As Tiago says, putting an #property declaration inside a class extension in the .m (implementation) file, is a way to make the property private so only the class itself can access it. When declared in the .h (public interface file), it is visible to all code that imports that .h file. Keep in mind that #properties are really just a convenience for declaring and synthesizing accessor methods, and as with all methods in Objective-C, they're never truly private. The best you get is a compiler warning that no public interface declares the method in question if you try to use a non-public method in another class.
For the second part of your question, this declares an instance variable ("ivar") called myString:
#interface MyClass : NSObject
{
NSString *myString;
}
While this declares a property called myString:
#property NSString *myString;
The difference between an instance variable and an #property is more significant than just saying that an ivar is accessible only by your class's instances. Declaring an ivar adds a variable to a class's structure in memory. In contrast, #properties declare/define methods on a class. By default, these methods set/get the value of an associated, and similarly named ivar, but that's not a requirement, and it is perfectly acceptable and quite common to have methods for an #property that don't access an ivar directly. Say for example a class that has a firstName and lastName properties backed by _firstName and _lastName ivars, along with a third, fullName property that simply concatenates the value returned by the firstName and lastName getter methods together (and/or splits a two part name in its set method).
A summarized explanation
A #property inside the .m is private to the class and in the .h is public.
The difference between declaring inside brackets or without brackets is the meaning of the variable.
Inside the brackets you declare an instance variable (or ivar) that is meant to be acessible only by your classes' instances. A property (declared in the .h) is meant to be acessible by any class.
Properties
A #property essentially defines a set and a get that you can override. When you do:
AppDelegate myAppDelegate;
myAppDelegate.hat = #"A hat":
You are essencially doing:
[myAppDelegate setHat:#"A hat"]
And when you do
myAppDelegate.hat //so you can get the property's value
you are essencially doing
[myAppDelegate hat]
Overriding Sets and Gets
When you do #synthesize hat = _hat you are essentially creating a get and a set that has an instance variable _hat associated. This instance variable should only be accessed in your gets/sets and even inside your class you should use your sets/gets (using self.property)
You can override the set and get created by the #synthesize overriding the following methods:
- (void) setHat:(NSString*) aHat
- (NSString *) hat
If you have a property in your public interface like the following
#interface MyClass : NSObject
#property(strong) NSString *myProp;
#end
And then synthesize it, in effect synthesizing the variable:
#implementation MyClass
#synthesize myProp = _myProp; // or just leave it at the default name..
#end
What is the visibility of the instance variable _myProp? That is, is this considered #public, #protected or #private? I'm guessing since MySubClass could inherit from MyClass then it would also get the properties (naturally), but would it also inherit the instance variable visibility?
What difference does it make if I put the property in a class extension? That would hide the property from subclasses, and I'm guessing the instance variable, too. Is this documented anywhere?
A synthesized ivar is completely invisible to all code that cannot see the #synthesize line (which basically means anything outside of the .m file). It's not #protected, it's not #private, it's simply unknown. With a #private ivar, other code trying to access it will be told that it's private, but with a synthesized ivar, other code trying to access it will be told that the field simply doesn't exist.
As a thought experiment, try imagining a situation where the ivar acted like it was #protected. You make a subclass, and you muck about with the ivar there. Now you go back to the superclass and change #synthesize myProp to #synthesize myProp=foo. What happens in the subclass? When the compiler processes the subclass, it cannot see the #synthesize line, so it would have no idea that you just changed the name of the ivar. In fact, it cannot even tell if the property is backed by an ivar at all, or if it's implemented with custom-written accessor methods. I hope it's obvious why this means that the subclass cannot possibly access the ivar, and neither can any other class.
That said, I'm not quite sure what the compiler does if you write code in the same .m file that tries to access the ivar. I expect it will treat the ivar as #private (since the compiler can, in fact, see that the ivar exists).
Also, none of this has any bearing on the runtime methods. Other classes can still use the obj-c runtime methods to dynamically look up your class's ivar list and muck about with it.
If it is declared in your interface it is virtually public when using the #property declarative. If you want to use #property declaratives and keep them property truly private, you should create a private category in your implementation.
MyClass.h
#interface MyClass : NSObject {
#private
NSObject* foo;
}
#end
MyClass.m
#import "ClassWithPrivateProperty.h"
#interface MyClass ()
#property (nonatomic,retain) NSObject* foo;
#end
#implementation MyClass
#synthesize foo;
// class implementation...
#end
A synthesized variable acts as if declared #private:
#interface Garble : NSObject
#property (copy) NSString * s;
#end
#implementation Garble
#synthesize s;
#end
#interface Bargle : Garble
#end
#implementation Bargle
- (void) useS {
NSLog(#"%#", s); // error: instance variable 's' is private
}
#end
I swear I've seen this in the docs, but I can't find it right now. Will update if I track it down.
You can create a dynamic property and indicate it to the compiler that its instantiation would be at run time.
And then in your subclass write your own getter or synthesize the property.
#interface BaseClass : NSObject
#property (nonatomic, strong) NSString *ThisWillBeSynthesizedInRespectiveSubclasses;
#end
#implementation BaseClass
#dynamic ThisWillBeSynthesizedInRespectiveSubclasses;
#end
In Sub classes
#interface Subclass : BaseClass
#end
#implementation Subclass
#synthesize ThisWillBeSynthesizedInRespectiveSubclasses = _ThisWillBeSynthesizedInRespectiveSubclasses;
#end
or you write your own setter / getter methods.
Hope this helps !
Other classes have access to everything that they #include. In other words, to everything that is inside your header.
If something appears only in your implementation file, other classes (including subclasses) don't know it exists. A synthesized property is like that. Other classes know only about the property (a property means a getter and a setter method) but they don't know anything about the inner implementation of its methods.
Note, that the access specifiers (public/private/protected) in obj-c are only a hint to the compiler that even if something appears in the header file, it can't be accessed. The runtime does not check it in any way.
What happens if you put it into a class extension? Note that a property is a set of two methods. You just hide the methods from every class which includes your class main header but not the class extension header.
We use this for example to declare a property as readonly and in class continuation we declare it as readwrite. Then, we can use the setter only from inside of the class.
If I have a class with some IBOutlets, it seems kind of silly to create accessors for these.
But then I feel like I'm breaking OO practices by not always going through the accessors for instance variables.
I also feel the same way about some instance variables that should not be public, I'd rather not expose the inner workings of some classes. I can make the actual ivars private but the #property shorthand doesn't seem to be able to specify visibility. This leads me to not create accessors and just access the ivars directly. I'm not sure if this is frowned upon though. Is it?
What's the community's thoughts on this admittedly newbie question? (Please ignore dot syntax)
I'm not sure about accessing instance variables directly, I think one shouldn't, but for some variables it just doesn't make sense to use accessors. Like for the IBOutlets you mentioned.
I can only help you out with private accessors. Starting with Objective-C 2.0 you can declare extensions.
Class extensions are like “anonymous”
categories, except that the methods
they declare must be implemented in
the main #implementation block for the
corresponding class.
Just put this extension into a separate header file and you'll have private accessors that aren't visible in the header.
Public/Private
You can declare your iVars as in the #interface file to be readonly, but then re-declare them in a category so that your class can change them. Here's a quick intro to Categories.
An example:
//MyClass.h
#interface MyClass : NSObject {
NSString *name;
}
#property (readonly) NSString *name;
#end
And in the implementation file you can redeclare this:
//MyClass.m
#interface MyClass () //declare the class extension
#property (readwrite, copy) NSString *name; //redeclare the property
#end
#implementation MyClass
#synthesize name;
#end
Now, the name property is readonly external to the class, but can be changed by the class through property syntax or setter/getter syntax.
Really private iVars
If you want to keep iVars really private and only access them directly without going through #property syntax you can declare them with the #private keyword. But then you say "Ah, but they can always get the value outside the class using KVC methods such as setValueForKey:" In which case take a look at the NSKeyValueCoding protocol class method + (BOOL)accessInstanceVariablesDirectly which stops this.
IBOutlets as properties
The recommended way is to use #property and #synthesize. For Mac OS X, you can just declare them as readonly properties. For example:
//MyClass.h
#interface MyClass : NSObject {
NSView *myView;
}
#property (readonly) IBOutlet NSView *myView;
#end
//MyClass.m
#implementation MyClass
#synthesize myView;
#end