ios - Can't create global function in Objective - C -
i'm trying create global function in objective-c. created new .m , h. file subclass of viewcontroller.
this connection.m
#import "connection.h" @interface viewcontroller () <nsstreamdelegate> @property (strong,nonatomic) nsinputstream * inputstream; @property (strong,nonatomic) nsoutputstream * outputstream; @end @implementation viewcontroller(connection) -(void)initnetworkcommunication { cfreadstreamref readstream; cfwritestreamref writestream; cfstreamcreatepairwithsockettohost(null, (cfstringref)@"192.168.0.10", 35000, &readstream, &writestream); self.inputstream = objc_unretainedobject(readstream); self.outputstream = objc_unretainedobject(writestream); [self.inputstream setdelegate:self]; [self.outputstream setdelegate:self]; [self.inputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [self.outputstream scheduleinrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [self.inputstream open]; [self.outputstream open]; }
connection.h
#import "viewcontroller.h" @interface viewcontroller(connection) -(void)initnetworkcommunication; @end
viewcontroller.m
#import "viewcontroller.h" #import "connection.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)connectionbutton:(id)sender { [self initnetworkcommunication]; } @end
i tryed 3 methods mentioned in how create global functions in objective-c no 1 of 3 works. first two(create class method in static class , declare global function instead of class) don't work because when change "-(void)... +(void).. every "self..." gets error , same in second method. third 1 works me (adding category nsobject methods) when i'm trying call [self initnetworkcommunication] function program crashesh signal sigabrt , can't solve problem. ideas how can call function?
update: @gronzzz
connection.m @interface viewcontroller () <nsstreamdelegate> @property (strong,nonatomic) nsinputstream * inputstream; @property (strong,nonatomic) nsoutputstream * outputstream; @end @implementation connection -(void)initnetworkcommunication connection.h #import "viewcontroller.h" @interface connection : viewcontroller -(void)initnetworkcommunication; @end viewcontroller.h #import "viewcontroller.h" #import "connection.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (void)callmethod { connection *detailvc = [[connection alloc] init]; [detailvc initnetworkcommunication]; } - (ibaction)connectionbutton:(id)sender { [self callmethod]; } @end
and signal sigabrt show when [self callmethod] called.
to call 1 instance method class, need initialize class , them call. calling method must inside @interface brackets. don't understand why did call "[self initnetworkcommunication]", needn't category or else.
detailviewcontroller.h
@interface detailviewcontroller : uiviewcontroller -(void)initnetworkcommunication; @end
detailviewcontroller.m
@implementation detailviewcontroller -(void)initnetworkcommunication { // } @end
masterviewcontroller.h
@implementation masterviewcontroller - (void)callmethod { detailviewcontroller *detailvc = [[detailviewcontroller alloc] init]; [detailvc initnetworkcommunication]; } @end
update
in .h file making interface category, in .m file making class extension , implementation of class, still doesn't makes interface class.
Comments
Post a Comment