How do we hide private class when using it in Objective-C? - ios

Question
How do we hide private class when using it in Objective-C?
For example, as described below
PrivateFilter is the class I want to hide.
CustomFilter is the class I make, which is open.
GPUImageFilter is the class public on Github, which is open, too.
And CustomFilter actully wraps the functionallity of PrivateFilter.
#interface CustomFilter : GPUImageFilter
#end
#interface PrivateFilter : GPUImageFilter
#end
Possible Solutions
So there are two solutions:
Solution 1: Class cluster
Have a look at the example A Composite Object: An Example in the Apple document.
I followed the steps and make the following code.
// CustomFilter.h
#interface CustomFilter : GPUImageFilter
#end
// CustomFilter.m
#interface CustomFilter()
#property (nonatomic, strong) PrivateFilter *privateFilter;
#end
#implementation CustomFilter
- (instancetype)init
{
self = [super init];
if (self) {
_privateFilter = [[PrivateFilter alloc] init];
}
return self;
}
// and then override the most of GPUImageFilter functions...
#end
But somehow it doesn't work, so I try the solution 2.
Solution 2: Init with PrivateFilter
// CustomFilter.h
#interface CustomFilter : GPUImageFilter
#end
// CustomFilter.m
#implementation CustomFilter
- (CustomFilter *)init
{
self = (CustomFilter *)[[PrivateFilter alloc] init];
return self;
}
#end
This works, but it's very strange to use alloc in init.
So I try the solution 3.
Solution 3: factory class method
// CustomFilter.h
#interface CustomFilter : GPUImageFilter
+ (CustomFilter *)filter;
#end
// CustomFilter.m
#implementation CustomFilter
+ (CustomFilter *)filter
{
CustomFilter *filter = (CustomFilter *)[[PrivateFilter alloc] init];
return filter;
}
#end
This works, but it could not be inherited, just like class cluster.
Repeat Question
SO which is the best solution? Or is there other some good solutions?

I think you need protocol, instead of CustomFilter with factory approach. If you need some GPUImageFilter subclass you can make GPUImageFilter<CustomFilter>
//CustomFilter.h
#protocol CustomFilter
...
#end
typedef GPUImageFilter<CustomFilter> CustomFilter;
//PrivateFilter.h
#import "CustomFilter.h"
#interface PrivateFitler: CustomFilter
...
#end
//GPUImageFilter+CustomFilter.h
#interface GPUImageFilter(CustomFilter)
+ (CustomFilter *)filter;
#end
//GPUImageFilter+CustomFilter.m
#import "PrivateFilter.h"
#implementation GPUImageFilter(CustomFilter)
+ (CustomFilter *)filter
{
return [[PrivateFilter alloc] init;
}
#end
With such approach Xcode even give you warnings if you forget to implement some methods.

Related

I need to pass a string from a NSObject class to a UIViewController with a delegate

I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.
A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.
I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.
I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.
my delegator class TemporarySelection.h is
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
#class DieFacesViewController;
#protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
#end
#interface TemporarySelection : NSObject
#property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
#property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
#end
and my TemporarySelection.m is
#import "TemporarySelection.h"
#implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
#end
the delegate class DiewFacesViewController.h is
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
#interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
#property (strong, nonatomic) IBOutlet UILabel *SelectionName;
#end
and the DieFacesViewController.m is
#import "DieFacesViewController.h"
#interface DieFacesViewController ()
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
You are not setting the delegate object properly.Check the above code
#import "DieFacesViewController.h"
#interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object
TemporarySelection *ts;
}
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
//Use the object to extract
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}

count NSArray from another class

Very basic question here...
I have 2 classes and I want to keep an NSArray in one class and access it in different class.
Questions.h
#import <Foundation/Foundation.h>
#interface Questions : NSObject
#property NSMutableArray *questions;
-(void) questionMethod;
#end
Questions.m
#import "Questions.h"
#implementation Questions
-(void) questionMethod {
NSArray *questionBank = [4,5,6];
}
#end
ViewController.m
#import "Questions.h"
-(void)generateRandomQuestionOrder {
Questions *questions = [[Questions alloc] init];
}
How do I count the values of questionBank array in generateRandomQuestionOrder method?
Not the best naming convention but take a look at the following example. You declare a public property in the Questions object and access it from the controller after you initialised a new object there. You may consider declaring it readonly and set it to readwrite in the private interface extension.
Questions.h
#import <Foundation/Foundation.h>
#interface Questions : NSObject
#property NSMutableArray *questions;
#end
Questions.m
#import "Questions.h"
#implementation Questions
-(id) init {
if (self = [super init]) {
_questions = #[4,5,6];
}
return self;
}
#end
ViewController.m
#import "Questions.h"
-(void)generateRandomQuestionOrder {
Questions *theQuestions = [[Questions alloc] init];
NSLog(#"%#", [theQuestions.questions description]);
}

Proper subclassing of class clusters

After watching iOS tech talks and reading up on class clusters I decided to extract legacy iOS 6 code to a private subclass:
#interface MyUIView : UIView #end // public
#interface MyUIViewiOS6 : MyUIView #end // private
#interface MyUIViewiOS7 : MyUIView #end // private
#implementation MyUIView
+ (id)alloc
{
// Don't loop on [super alloc]
if ([[self class] isSubclassOfClass:[MyUIView class]] &&
([self class] != [MyUIViewiOS6 class]) &&
([self class] != [MyUIViewiOS7 class]))
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
return [MyUIViewiOS6 alloc];
} else {
return [MyUIViewiOS7 alloc];
}
}
return [super alloc];
}
// Common implementation
#end
#implementation MyUIViewiOS6
// Legacy code
#end
#implementation MyUIViewiOS7
// iOS specific code
#end
This implementation works well until I want subclass MyUIView. For example if I create a subclass:
#interface MyRedUIView : MyUIView #end
and then init it like that:
[[MyRedUIView alloc] init]
object of type MyUIViewiOS6 or MyUIViewiOS7 will be allocated instead. Is there a way I can adapt this pattern to support subclassing, so that superclass of MyRedUIView is dynamically switched to MyUIViewiOS6 or MyUIViewiOS7?
You've reached the classic double-inheritance problem. You want to be either a RedUIView or GreenUIView and be either a MyUIViewiOS6 or a MyUIViewiOS7 view.
Since objective-c does not support double-inheritance, you'll have to decide the difference between what you are, and how you act. Anything that determines what you are, you put in the class. Anything that determines how you act goes into a #protocol which then can be implemented.
I would subclass MyUIView since MyUIViewiOS6 and MyUIViewiOS7 correspond to who you are, and then implement a Red or Green protocol for certain functionality:
#interface MyRedUIView : MyUIView<RedProtocol> #end
You can check to see if this class conforms to a specific protocol:
if ([class conformsToProtocol:#protocol(RedProtocol)]) {
self.color = [UIColor redColor];
}
If both of them really are who you are, then you have to use four separate classes.
Here's an example using categories. Assuming that you have MyUIView as specified in the question:
GreenView.h
#import "MyUIView.h"
#import "Green.h"
#interface MyUIView (GreenUIView) <Green>
-(BOOL) isGreen;
#end
#interface GreenView : MyUIView #end
GreenView.m
#import "GreenView.h"
#implementation MyUIView (GreenUIView)
-(BOOL) isGreen{
return [self conformsToProtocol:#protocol(Green)];
}
#end
#implementation GreenView #end
Green.h
#protocol Green <NSObject> #end
AppDelegate.m
#import "GreenView.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
GreenView* view = [[GreenView alloc] init];
NSLog(#"%#", [view isGreen]?#"yes":#"no");
return YES;
}
#end

objective-c multi protocols in one class correct syntax

I've a main class where I want to define two protocols (1 used by a class A, the other by class B) (ios 6.1, xcode 4.6.3 , ARK mode, storyboard project).
According to official syntax, all my code seems to be correct.
But when I try to use the second delegate , nothing work correctly, my 2nd delegate does not respond
**HEADER myProtocols.h**
#import ...
#class myProtocols;
#protocol myProtocol1 <NSObject>
// list of methods and properties
doStuff:(float) myValue;
#end
#protocol myProtocol2 <NSObject>
// list of methods and properties
doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
#end
#interface myProtocols:NSObject
{
__unsafe_unretained id <myProtocol1> _myDelegate1;
__unsafe_unretained id <myProtocol2> _myDelegate2;
}
#property (nonatomic, assign) id <myProtocol1> myDelegate1;
#property (nonatomic, assign) id <myProtocol2> myDelegate2;
#end
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
...
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE DONT WORK, IT'S LIKE IT DOESNT INIT**
...
#end
**HEADER classA.h**
#import "myProtocols.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doStuff:(float) myValue; according to comments, nothing to do :(
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
**HEADER classB.h**
#import "myProtocols.h"
#interface classB: UIViewController <myProtocol2>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType; according to comments, nothing to do :(
#end
**MESSAGES classB.m**
#import "classB.h"
#interface classB ()
#end
#implementation classB
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate2 = self;
}
-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
{
NSLog(#" YES VALUE IS %f and text %# and type %#",myValue2,myText,myType);
}
So, my mistake is to call [_myDelegate2 doOtherStuff..] directly inside a function in myProtocols called only by classA.
Then, if I want to call a function in myProtocols using both delegate I MUST init both these delegates in the class (A or B doesnt matter) I use to call this function:
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
-(void) pleaseDoIt
{
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE NOW WORK VERY WELL**
}
#end
**HEADER classA.h**
#import "myProtocols.h"
#import "classB.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
#property(strong, nonatomic) classB *classBVC;
//-(void) doStuff:(float) myValue;
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_classBVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
_myProtoVC.myDelegate2 = _classBVC // THIS IS THE POINT!!!
[_myProtoVC pleaseDoIt];
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}

Assertion in Helper Class Ignored

I have a test case and a helper class. In the helper class I want to use asserts too like here:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
#interface MainTests : SenTestCase
#end
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
#implementation MainTests
- (void)testExample {
HelperClass *helperClass = [[HelperClass alloc] init];
[helperClass fail];
}
#end
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
#interface HelperClass : SenTestCase
- (void)fail;
#end
HelperClass.m
#import "HelperClass.h"
#implementation HelperClass
- (void)fail {
STFail(#"This should fail");
}
#end
Sidenote: I had to make the helper class a subclass from SenTestCase to being able to access the assertion macros.
The assertion from the helper class is ignored. Any ideas why? How can I use assertions in helper classes?
I had this same problem today and came up with a hack that worked for my purposes. Poking into the SenTestCase macros, I noticed that they call [self ...] on the helper but didn't trigger the asserts. So, wiring up the source class to the helper got it working for me. Changes to your question classes would look like:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
#interface MainTests : SenTestCase
#end
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
#implementation MainTests
- (void)testExample {
// Changed init call to pass self to helper
HelperClass *helperClass = [[HelperClass alloc] initFrom:self];
[helperClass fail];
}
#end
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
#interface HelperClass : SenTestCase
- (id)initFrom:(SenTestCase *)elsewhere;
- (void)fail;
#property (nonatomic, strong) SenTestCase* from;
#end
HelperClass.m
#import "HelperClass.h"
#implementation HelperClass
#synthesize from;
- (id)initFrom:(SenTestCase *)elsewhere
{
self = [super init];
if (self) {
self.from = elsewhere;
}
return self;
}
- (void)fail {
STFail(#"This should fail");
}
// Override failWithException: to use the source test and not self
- (void) failWithException:(NSException *) anException {
[self.from failWithException:anException];
}
#end
It is entirely possible that additional overrides are needed for more advanced functionality, but this did the trick for me.

Resources