How can I add values to NSArray of different class A from Class B - objective-c

I have my .m file where I need to update NSArray of class B, where I should be able to the values from that array.
I am trying this in Class A
// TabHeaderViewModel.h
typedef enum {
UsePassHeaderType,
ManageCardType
}TabHeaderType;
NS_ASSUME_NONNULL_BEGIN
#interface TabHeaderViewModel : NSObject
#property (nonatomic) TabHeaderType headerType;
- (instancetype)initWithHeaderType:(TabHeaderType)headerType;
- (NSArray *)dataSourceForHeaderModel;
#end
And now I want to access datatSourceForHeaderModel from class B in its .m file
TabHeaderViewModel *headerVM = [[TabHeaderViewModel alloc]initWithHeaderType:ManageCardType];
headerVM.dataSourceForHeaderModel = self.dataSourceForHeader;
// getting error - No setter method 'setDataSourceForHeaderModel:' for assignment to property

either code in interface
#property (nonatomic) NSArray *dataSourceForHeaderModel;
or
-(void)setDataSourceForHeaderModel:(NSArray*)datasource;
also do your implementation when you define a method if there is processing.
But its easier to go with the property solution.

Related

Private v/s Public class properties in Objective C

Trying to make my OOP fundamentals strong based of objective C. Sorry if my explanation is too long.
I have 3 classes in my app as Class A, Class B and Class C objective C classes.
I have a property of class A in my implementation of class B i.e. it makes private property of class B.
// implementation Class B
#interface ClassB ()
#property (nonatomic, strong) ClassA *classA;
#end
I create an instance of class B in one of my class C methods and try to access class A property through class B's instance in class C.
// implementation Class C
#interface ClassC ()
#property (nonatomic, strong) ClassB *classB;
#end
#implementation ClassC
- (void)someMethod
{
NSString *string = [[NSString alloc] init];
classB = [[ClassB alloc] init];
string = classB.classA.displayString; //get an error here - ClassB doesn't have classA.
}
#end
To avoid the error I moved the classA property from implementation to header in ClassB.
// header Class B
#interface ClassB : NSObject
#property (nonatomic, strong) ClassA *classA;
#end
But I am worried that anybody class can create an instance of class B, access classA property and can then use/modify the properties which are part of class A.
Question: Is it a good style to move the classA property to the header file of Class B so I can use it in Class C or should I create a method in Class B which returns me whatever I need from class A? Something like:
#implementation ClassB
- (NSString*)displayStringOfClassA
{
classA = [[ClassA alloc] init];
return self.classA.displayString;
}
#end
In ClassB.h:
#interface ClassB: NSObject
#property (nonatomic, strong, readonly) ClassA *classA;
#end
In ClassB.m:
#interface ClassB()
#property (nonatomic, strong, readwrite) ClassA *classA;
#end
Also, strong and readwrite are default modifiers - you can get rid of them. However, they improve code readability.
EDIT: if you want to forbid access for ClassA properties - do the same trick. Suggested above code will forbid only to modify classA property of classB. For ClassA's displayString:
In ClassA.h:
#interface ClassA: NSObject
#property (nonatomic, strong, readonly) NSString *displayString;
#end
In ClassA.m:
#interface ClassA()
#property (nonatomic, strong, readwrite) NSString *displayString;
#end
I would suggest a readonly string property in ClassB.h.
ClassB.h:
#property (nonatomic, readonly) NSString *classAString;
ClassB.m:
- (NSString *) classAString
{
return self.classA.displayString;
}
This acts as a "getter" method for the particular string you need, and avoids others getting access to classA.
Edit:
Others suggested adding classA as a readonly property in ClassB.h. This will still allow modification of classA properties, it will only guarantee that classA is not reassigned to another ClassA instance.
Sounds like you want "framework", not "private" #property() declarations.
To do this, create a file like:
ClassA_Private.h
That contains your #property() declaration.
Then #import "ClassA_Private.h" in both your ClassA.m (prior to the #implementation) and in any subclasses that want to use that #property.
This was a secondary design consideration when creation class extensions; adding the ability to have #property declarations that are fully accessible across subclasses and/or within frameworks without being exposed externally. While you can't create a framework for an iOS targeted application, the same functionality still applies.
Something like string = objectB.objectA.displayString; is violating the Law of Demeter. Designing according to the Law of Demeter results in more maintainable and adaptable software.
You should always try to not talk to strangers. That is in your example: self talks to objectB and objectB talks to objectA but self shouldn't talk to objectA because it's a stranger.

Expose a private Objective-C method or property to subclasses

According to some official talk, a class in Objective-C should only expose public methods and properties in its header:
#interface MyClass : NSObject
#property (nonatomic, strong) MyPublicObject *publicObject;
- (void)publicMethod;
#end
and private methods/properties should be kept in class extension in .m file:
#interface MyClass()
#property (nonatomic, strong) MyPrivateObject *privateObject;
- (void) privateMethod;
#end
and I don't think there is a protected type for things that are private but accessible from subclasses. I wonder, is there anyway to achieve this, apart from declaring private properties/methods publicly?
One way to solve this is to re-declare the property in your subclass's class extension, and then add an #dynamic statement so that the compiler won't create an overriding implementation of that property. So something like:
#interface SuperClass ()
#property (nonatomic, strong) id someProperty;
#end
....
#interface SubClass ()
#property (nonatomic, strong) id someProperty;
#end
#implementation SubClass
#dynamic someProperty;
#end
This obviously isn't ideal because it duplicates a privately visible declaration. But it is quite convenient and helpful in some situations so I'd say evaluate on a case-by-case basis the dangers involved in this duplication vs. exposing the property in the public interface.
An alternative - that is used by Apple in UIGestureRecognizer - is to declare the property in a separate category header file explicitly named as "private" or "protected" e.g. "SomeClass+Protected.h". That way, other programmers will know they ought not import the file. But, if you don't control the code you're inheriting from, that's not an option.
This is possible by using a class extension (not category) that you include in the implementation files of both the base class and subclasses.
A class extension is defined similar to a category, but without the category name:
#interface MyClass ()
In a class extension, you can declare properties, which will be able to synthesize the backing ivars (XCode > 4.4 automatic synthesis of the ivars also works here).
In the extension class, you can override/refine properties (change readonly to readwrite etc.), and add properties and methods that will be "visible" to the implementation files (but note that the properties and methods aren't really private and can still be called by selector).
Others have proposed using a seperate header file MyClass_protected.h for this, but this can also be done in the main header file using #ifdef like this:
Example:
BaseClass.h
#interface BaseClass : NSObject
// foo is readonly for consumers of the class
#property (nonatomic, readonly) NSString *foo;
#end
#ifdef BaseClass_protected
// this is the class extension, where you define
// the "protected" properties and methods of the class
#interface BaseClass ()
// foo is now readwrite
#property (nonatomic, readwrite) NSString *foo;
// bar is visible to implementation of subclasses
#property (nonatomic, readwrite) int bar;
-(void)baz;
#end
#endif
BaseClass.m
// this will import BaseClass.h
// with BaseClass_protected defined,
// so it will also get the protected class extension
#define BaseClass_protected
#import "BaseClass.h"
#implementation BaseClass
-(void)baz {
self.foo = #"test";
self.bar = 123;
}
#end
ChildClass.h
// this will import BaseClass.h without the class extension
#import "BaseClass.h"
#interface ChildClass : BaseClass
-(void)test;
#end
ChildClass.m
// this will implicitly import BaseClass.h from ChildClass.h,
// with BaseClass_protected defined,
// so it will also get the protected class extension
#define BaseClass_protected
#import "ChildClass.h"
#implementation ChildClass
-(void)test {
self.foo = #"test";
self.bar = 123;
[self baz];
}
#end
When you call #import, it basically copy-pastes the .h file to where you are importing it.
If you have an #ifdef, it will only include the code inside if the #define with that name is set.
In your .h file, you don't set the define so any classes importing this .h wont see the protected class extention.
In the base class and subclass .m file, you use #define before using #import so that the compiler will include the protected class extension.
While the other answers are correct, I'd like to add...
Private, protected and public are available for instance variables as such:
#interface MyClass : NSObject {
#private
int varA;
#protected
int varB;
#public
int varC;
}
#end
Your only choice is to declare it as public in the header file. If you want to at least keep some method separation, you can create a category and have all your protected methods and attributes there, but in the end everything will still be public.
#import "MyClass.h"
#interface MyClass (Protected)
- (void) protectedMethods;
#end
Simply create a .h file with your class extension. Import this into your .m files. Incidentally, this is a great way to test private members without breaking encapsulation (I'm not saying you should test private methods :) ).
// MyClassProtectedMembers.h
#interface MyClass()
#property (nonatomic, strong) MyPrivateObject *privateObject;
- (void) privateMethod;
#end
/////////////////
#import "MyClassProtectedMembers.h"
#implementation MyClass
// implement privateMethod here and any setters or getters with computed values
#end
Here's a gist of the idea: https://gist.github.com/philosopherdog/6461536b99ef73a5c32a
I see good answers for making properties visible, but I don't see exposing the methods addressed very clearly in any of these answers. Here is how I have successfully exposed private methods to the subclass using a Category:
SomeSuperClass.m:
#implementation SomeSuperClass
-(void)somePrivateMethod:(NSString*)someArgument {
...
}
SomeChildClass.h
#interface SomeChildClass : SomeSuperClass
SomeChildClass.m
#interface SomeSuperClass (exposePrivateMethod)
-(void)somePrivateMethod:(NSString*)someArgument;
#end
#implementation SomeChildClass
-(void)doSomething {
[super somePrivateMethod:#"argument"];
}
#end
That's because there's not even a real distinction between private and public. While the compiler may warn you about an interface missing a certain method or instance variable, your program will still work.

NSObject subclass as a property

I want to use my class as a property in my project. The idea is that i have a class which contains all list ellements. The basic idea i show below in graph:
So i have a myContainerClass object, and i want to do in some other class:
#property (strong,nonatomic) MyContainerClass *obj;
and here i have error! I figure out that i can only use Foundations type as a #property. But Why? What is replacement for doing that (passing an object)?
No, you can use any class you like as a property
#property (nonatomic, strong) MyContainerClass* obj;
is perfectly legal provided that the compiler knows that MyContainerClass is a class. To do that in the header file, the best way is to use an #class forward declaration:
#class MyContainerClass;
#interface SomeOtherClass : NSObject
// method an property declarations
#property (nonatomic, strong) MyContainerClass* obj;
#end
And then include the header file in the implementation:
#import "MyContainerClass.h"
#implementation SomeOtherClass
#synthesize obj;
// other stuff
#end
What is the error you are getting? May be you are not importing MyContainerClass to where you want to use it.
#import "MyContainerClass.h"
Declare a category for an object that you want to add your property to:
#interface NSObject (MyContainerClassAdditions)
#property (nonatomic, strong) MyContainerClass *myContainerClass
#end
Then implement the setter and getter methods using objective c associated object trick:
#import <objc/runtime.h>
#implementation NSObject (MyContainerClassAdditions)
- (void)setMyContainerClass:(MyContainerClass *)myContainerClass {
objc_setAssociatedObject(self, "myContainerClass", myContainerClass, OBJC_ASSOCIATION_ASSIGN);
}
- (MyContainerClass *)myContainerClass {
return objc_getAssociatedObject(self, "myContainerClass");
}
#end

private property in Objective C

Is there a way to declare a private property in Objective C? The goal is to benefit from synthesized getters and setters implementing a certain memory management scheme, yet not exposed to public.
An attempt to declare a property within a category leads to an error:
#interface MyClass : NSObject {
NSArray *_someArray;
}
...
#end
#interface MyClass (private)
#property (nonatomic, retain) NSArray *someArray;
#end
#implementation MyClass (private)
#synthesize someArray = _someArray;
// ^^^ error here: #synthesize not allowed in a category's implementation
#end
#implementation MyClass
...
#end
I implement my private properties like this.
MyClass.m
#interface MyClass ()
#property (nonatomic, retain) NSArray *someArray;
#end
#implementation MyClass
#synthesize someArray;
...
That's all you need.
A. If you want a completely private variable. Don't give it a property.
B. If you want a readonly variable that is accessible external from the encapsulation of the class, use a combination of the global variable and the property:
//Header
#interface Class{
NSObject *_aProperty
}
#property (nonatomic, readonly) NSObject *aProperty;
// In the implementation
#synthesize aProperty = _aProperty; //Naming convention prefix _ supported 2012 by Apple.
Using the readonly modifier we can now access the property anywhere externally.
Class *c = [[Class alloc]init];
NSObject *obj = c.aProperty; //Readonly
But internally we cannot set aProperty inside the Class:
// In the implementation
self.aProperty = [[NSObject alloc]init]; //Gives Compiler warning. Cannot write to property because of readonly modifier.
//Solution:
_aProperty = [[NSObject alloc]init]; //Bypass property and access the global variable directly
It depends what you mean by "private".
If you just mean "not publicly documented", you can easily enough use a class extension in a private header or in the .m file.
If you mean "others are not able to call it at all", you're out of luck. Anyone can call the method if they know its name, even if it is not publicly documented.
As others have indicated, (currently) there is no way to truly declare a private property in Objetive-C.
One of the things you can do to try and "protect" the properties somehow is to have a base class with the property declared as readonly and in your subclasses you can redeclare the same property as readwrite.
Apple's documentation on redeclared properties can be found here: http://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW19

Properties for Class and Its Subclasses Only

Is it possible to define properties that are only available to the class they are defined in, and that class's subclasses?
Stated another way, is there a way to define protected properties?
Technically, no. Properties are really just methods, and all methods are public. The way we "protect" methods in Objective-C is by not letting other people know about them.
Practically, yes. You can define the properties in a class extension, and still #synthesize them in your main implementation block.
This is possible by using a class extension (not category) that you include in the implementation files of both the base class and subclasses.
A class extension is defined similar to a category, but without the category name:
#interface MyClass ()
In a class extension, you can declare properties, which will be able to synthesize the backing ivars (XCode > 4.4 automatic synthesis of the ivars also works here).
In the extension class, you can override/refine properties (change readonly to readwrite etc.), and add properties and methods that will be "visible" to the implementation files (but note that the properties and methods aren't really private and can still be called by selector).
Others have proposed using a seperate header file MyClass_protected.h for this, but this can also be done in the main header file using #ifdef like this:
Example:
BaseClass.h
#interface BaseClass : NSObject
// foo is readonly for consumers of the class
#property (nonatomic, readonly) NSString *foo;
#end
#ifdef BaseClass_protected
// this is the class extension, where you define
// the "protected" properties and methods of the class
#interface BaseClass ()
// foo is now readwrite
#property (nonatomic, readwrite) NSString *foo;
// bar is visible to implementation of subclasses
#property (nonatomic, readwrite) int bar;
-(void)baz;
#end
#endif
BaseClass.m
// this will import BaseClass.h
// with BaseClass_protected defined,
// so it will also get the protected class extension
#define BaseClass_protected
#import "BaseClass.h"
#implementation BaseClass
-(void)baz {
self.foo = #"test";
self.bar = 123;
}
#end
ChildClass.h
// this will import BaseClass.h without the class extension
#import "BaseClass.h"
#interface ChildClass : BaseClass
-(void)test;
#end
ChildClass.m
// this will implicitly import BaseClass.h from ChildClass.h,
// with BaseClass_protected defined,
// so it will also get the protected class extension
#define BaseClass_protected
#import "ChildClass.h"
#implementation ChildClass
-(void)test {
self.foo = #"test";
self.bar = 123;
[self baz];
}
#end
When you call #import, it basically copy-pastes the .h file to where you are importing it.
If you have an #ifdef, it will only include the code inside if the #define with that name is set.
In your .h file, you don't set the define so any classes importing this .h wont see the protected class extention.
In the base class and subclass .m file, you use #define before using #import so that the compiler will include the protected class extension.
You could use such syntax in subclass implementation.
#interface SuperClass (Internal)
#property (retain, nonatomic) NSString *protectedString;
#end
You can use a category to gain your purpose
#interface SuperClass (Protected)
#property (nonatomic, strong) UIImageView *imageView;
#property (nonatomic, strong) UIView *topMenuView;
#property (nonatomic, strong) UIView *bottomMenuView;
#end
In Subclass, you import this category in the file .m