0

Suppose I am creating a static library and in which I have two NSObject Classes Named as ClassA and ClassB, User can Access ClassA.h to execute the functions inside library. Now I've some functions defined into ClassB and I want user to execute these functions from their ViewController as per their need but I don't want to give ClassB.h to user so how user can access it using ClassA.

ClassA.h

#import <Foundation/Foundation.h>

@interface ClassA : NSObject

+ (void) method_1;
+ (void) method_2;
+ (void) method_3;
//  Tried to pass Instance of ClassB to user ViewController

+ (id) getClassBInstanceObject;

@end

ClassA.m

#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

+ (void) method_1 {
//Some Stuffs Done Here !!
}

+ (void) method_2 {
//Some Stuffs Done Here !!
}

+ (void) method_3 {
//Some Stuffs Done Here !!
}

//  Tried to pass Instance of ClassB to user ViewController

+ (id) getClassBInstanceObject {
 id objB = [ClassB sharedInstance];
    return objB;
}

@end

ClassB.h

#import <Foundation/Foundation.h>

@interface ClassB : NSObject

+ (id)sharedInstance;
-(void)Test_ClassB_function;
@end

ClassB.m

#import "ClassB.h"

@implementation ClassB

+ (id)sharedInstance {
    static ClassB *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    });
    return instance;
}
-(void)Test_ClassB_function {
    NSLog(@"this is the method called from ClassB !!!!");
}

@end

Now User Have a Static library FAT (lib.a) file and ClassA.h File Which He'll include in his project and then he can import ClassA.h into his ViewController Class to execute library functions and user wants to execute Test_ClassB_function using ClassA.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

ViewController.m

#import "ViewController.h"
#import "ClassA.h"

@interface ViewController ()

@end

@implementation ViewController

  override func viewDidLoad() {
        super.viewDidLoad()
[ClassA method_1];
[ClassA method_2];
[ClassA method_3];
}

- (IBAction)Button1:(id)sender {
id class_B_Object = [ClassA getClassBInstanceObject];

[class_B_Object Test_ClassB_function];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

Can anybody tell me how to achieve this goal in Objective-C.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mohammad Ashraf Ali
  • 417
  • 1
  • 8
  • 22
  • Methods should not be o]prefixed with get nor should they contain _. – bbum Dec 04 '17 at 14:09
  • I was aware they shouldn’t start with underscores, what is the rationale behind avoiding underscores inside? Sure, in general you should camelCase, but I’ve used it in the past to map other identifiers to selectors. Is that dangerous? – uliwitness Dec 04 '17 at 18:53
  • Not dangerous, just nonstandard. There are cases for it here and there, but not all over the place as it is here. – bbum Dec 04 '17 at 20:35

1 Answers1

0

Maybe try

In ClassA.h

+ (void) testClassBFunctionFromClassA;

In ClassA.m

+ (void) testClassBFunctionFromClassA{

    [[ClassB sharedInstance] Test_ClassB_function];

}

In ViewController.m

- (IBAction)Button1:(id)sender {
    [ClassA testClassBFunctionFromClassA];
}
  • This is a temporary solution In this case I need to create different method for all ClassB method into ClassA. And If ClassB Object accessible then no need to create this – Mohammad Ashraf Ali Dec 04 '17 at 06:47