How do you differentiate private member variable in objective-c? - iphone

for private methods,
I could use
#interface MyClass(PrivateMethods)
- (void) _foo;
#end
How do you declare a private member variable in objective c?

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html#//apple_ref/doc/uid/TP30001163-CH12-TPXREF127
#interface MyClass : Superclass {
#private
NSString * privateString;
}
#end

You can declare it as follows..
#private //variable;
Check this question out.
Question- private-instance-variable

Related

Xcode 6 - No Visible interface for private members

I have declared private variables in implementation file of cycles.
myclass.m
#interface myclass()
#property (nonatomic) unsigned int number;
#end
Well, when I put in main.m something like this:
myclass * some = [[myclass alloc] init];
[some setNumber:10]; // no visible #interface for 'myclass' declares the selector 'setNumber'.
unsigned int a=[some getNumber]; //no visible #interface ...
Every answer on StackOverflow.com points the same as I do. What is the problem?
You can use readonly/readwrite property definitions to achieve this.
In MyClass.h
#interface MyClass : NSObject
#property (nonatomic, readonly) NSNumber *number;
#end
In MyClass.m
#interface MyClass ()
#property (nonatomic, readwrite) NSNumber *number;
#end
You can now read the number property outside of the class, and you will only be able to change it inside of MyClass.m. You can set it's value like so, self.number = 5 inside of MyClass.m.
Objective-C does not have the same type of private variables that a language like Java has. What is it that you are trying to accomplish with a private variable?
The problem is that the property number is a private property because you declared it in the interface section in your .m file. Declare it in your header myclass.h files interface block instead.. A private property, ivar, or method is one which can only be accessed from within an instance of your myClass object by using the self keyword (which is basically a pointer to the myClass instance within which you are currently executing).
Moving your property declaration to the header file will make the property accessible from outside the class instance. In other words, it makes it public.
If you want to restrict access to the data the property points to, define the property in the header file as a readonly property:
MyClass.h
#interface MyClass : NSObject
#property (nonatomic, readonly) unsigned int number;
#end
Then in your .m file create a private ivar manually and override the getter for the number property as follows:
MyClass.m
#implementation MyClass {
//Instance variable (ivar)
unsigned int _number;
}
unsigned int number()
{
return _number;
}
#end
That way a subclass (or anyone else for that matter) can't muck with the value of number. You can then access number in your main function as follows:
MyClass* myClass = [[MyClass alloc] init];
unsigned int newNumber = myClass.number; //There is no such selector called getNumber BTW, only number.

How to declare variables in #protocol

In Java I have a class like this:
public interface Test {
public final static String TAG = "Tag";
}
And other classes are able to implement this Interface and use the declared variables in my interface!
Now I want to do the same thing in Objective C and i found Protocols and tried it like this:
#protocol Test <NSObject>
NSString *const TAG = #"Tag";
#end
But that didn't work! What's the correct way?!
UPDATE:
That Question here is exactly what i am looking for:
how to do it on objective-c: extending protocol and interface like in java
But thereĀ“s no proper answer!
Asking before researching is a terrible habit.
In Java public final static String TAG = "Tag"; is just as making a global constant in Objective-C. Just declare that out side the protocol.
static const NSString *TAG = #"Tag";
#protocol Test <NSObject>
-(...)...
#end
In the header file declare a global variable:
extern NSString * const kTAG;
In one implementation file define and initialize the variable:
NSString * const kTAG = #"Tag";
Like this:
#protocol Test <NSObject>
#property (readonly) NSString *TAG;
#end
and within the class that implements this protocol, you will need to provide:
- (NSString *)TAG {
return #"Tag";
}
If you want to just have NSString as property in protocol use that:
#protocol Test <NSObject>
#property (nonatomic, retain) NSString* TAG;
#end
If you want to use constant - which is not really a variable :) just put it outside protocol block:
static NSString* const *const TAG = #"Tag";

ios - How to assign a new class in existing class?

I'm new to iPhone development,I want to assign one class in existing class
This is what declare in C#
public partial class UserRegisteration : System.Web.UI.Page
{
//some methods
public class Mime
{
//some methods
}
}
like the above format in Objective-C how to assign one more class in existing class?
Any ideas? Thanks in advance.
Yes you can.I am using one class in existing class . One is extending UIView and other is extending NSObject.
MultiLayout.h
#interface MultiLayout : UIView
{
// methods and properties
}
#end
#interface SingleControl : NSObject
{
// methods and properties
}
#end
MultiLayout.m
#implementation SingleControl
//methods and variables declaration and property synthesization
#end
#implementation MultiLayout
//methods and variables declaration and property synthesization
#end
For getting the static value of SingleControl in MultiLayout . you have to call class method like:
MultiLayout.h
#interface MultiLayout : UIView
{
// methods and properties
}
#end
#interface SingleControl : NSObject
{
// methods and properties
}
// add class method
+(int)getValue;
#end
MultiLayout.m
#implementation SingleControl
// add static value
static int values = 100;
// implement method
+(int)getValue{
return values;
}
#end
#implementation MultiLayout
// call method to get value
[SingleChoiceControl getValue];
#end
If I understand your question correctly, you would do this in your interface file:
#interface Mime
//properties and method declarations
#end
#interface UserRegistration : System.Web.UI.Page
#property (strong, nonatomic) Mime *mimeInstance;
#end
and then in the implementation file you implement both like so:
#implementation Mime
//implementation
#end
#implementation UserRegistration
//implementation
#end

in Objective-C, how to access private property from category

I want to access private property of a class from its category.
But to access private property, I have to redeclare the same private property in category.
If I don't redeclare, I get a compile error, Property '<property name>' not found on object of type '<class name> *'.
Is this correct way to access private property of class from category?
And are there better ways to do this?
The following code is the code which private property is redeclared in category:
ClassA.h
#interface ClassA : NSObject
-(void)method1;
#end
ClassA.m
#import "ClassA.h"
// private property
#interface ClassA()
#property (nonatomic) NSString *s;
#end
#implementation ClassA
#synthesize s;
-(void)method1
{
self.s = #"a";
NSLog(#"%#", [NSString stringWithFormat:#"%# - method1", self.s]);
}
#end
ClassA+Category.h
#import "ClassA.h"
#interface ClassA(Category)
-(void)method2;
#end
ClassA+Category.m
#import "ClassA+Category.h"
// redeclare private property
#interface ClassA()
#property(nonatomic) NSString *s;
#end
#implementation ClassA(Category)
-(void)method2
{
NSLog(#"%#", [NSString stringWithFormat:#"%# - method2", self.s]);
}
#end
Is is good way to create a file(ClassA+Private.m) for private property and import it from ClassA.m and ClassA+Category.m:
ClassA+Private.m
#interface ClassA()
#property(nonatomic) NSString *s;
#end
The best way to solve this is to create ClassA+Private.h and import it in ClassA.m and Category.m. Mind the h at the end, you only need to declare your private properties and methods there, the definition is better kept in ClassA.m.

Can protocols within protocols be treated as inclusive of the protocol they adopt?

I am assigning protocols in a couple classes that follow an inheritance tree. Like so:
first class
#protocol LevelOne
- (void) functionA
#end
#interface BaseClass : NSObject <LevelOne> {
}
second class
#protocol LevelTwo <LevelOne>
- (void) functionB
#end
#interface SubClass : BaseClass <LevelTwo> {
}
Later I am assigning the class as delegate properties of other classes
base class
#interface AppClass : NSObject {
#protected
id<LevelOne> levelOneDelegate;
}
#property (assign) id<LevelOne> levelOneDelegate;
subclass
#interface AppClassesFriend : AppClass {
#protected
id<LevelTwo> levelTwoDelegate;
}
#property (assign) id<LevelTwo> levelTwoDelegate;
At the end of this journey, AppClassesFriend has 2 properties on it.
"levelOneDelegate" has access to "functionA", when it is assigned with a BaseClass object.
However, I am finding that "levelTwoDelegate" only has access to "functionB" (it is assigned with a SubClass object).
In order to have AppClassesFriend be able to use both functions, it seems I need to assign BOTH a levelOneDelegate AND levelTwoDelegate.
Is there any way to make "levelTwoDelegate" have access to both? Since, both functions are available on "SubClass".
So, what I would like to be able to do is :
SubClass *s = [SubClass alloc];
AppClassesFriend *a = [AppClassesFriend alloc];
a.levelTwoDelegate = s;
so inside AppClassesFriend (a) I could use :
[self.levelTwoDelegate functionA]; <---- this is never found
[self.levelTwoDelegate functionB];
but it seems I have to add
a.levelOneDelegate = s;
Thanks if anyone took the time to read all the way down this far. So in summary the question is, how do I get "levelTwoDelegate" to have access to both functionA and functionB?
Simply declare that your subclass's delegate property implements both level one and level two protocols (i.e. implements both functionA and functionB):
#interface AppClassesFriend : AppClass {
#protected
id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
}
#property (assign) id<LevelOne,LevelTwo> levelOneAndTwoDelegate;