Using Extended NSData Methods without Implementation - objective-c

I linked against a framework that implements extended methods on NSData.
The framework is not open-source so I don't have its implementation.
The extended methods are :
testheader.h
#import <Foundation/Foundation.h>
#interface NSData (TestObj)
- (id)test2:(id)arg1;
- (id)test:(id)arg1;
- (id)test3:(id)arg1;
#end
However, when I import that header file and try to use it that way:
#import "testheader.h"
#implementation ViewController
static void test_header_func () {
NSData *test_var = [NSData test];
}
I'm getting an error says
"No known class method for selector 'test'"

The category provides extra instance methods. You are attempting to use them as a class method. And you are not passing the required argument.
static void test_header_func () {
NSData *test_var = [NSData new]; // or some existing `NSData` instance you have
id someResult = [var test:someArgument];
}

Related

Call Swift class method from Objective-C

I am using Objective-C to consume a Swift Code that I am turning into a Framework/SDK eventually.
I can not invoke the class Methods specifically to Objective-C, and I am getting this error
No visible #interface for 'class' declares the selector 'method'
Swift File
public class Example {
public class func testOne() -> NSString {
return "testOne";
}
public func testTwo() -> NSString {
return "testTwo";
}
}
Objective-C File
#import <ProjectModuleName-Swift.h>
#interface ...
#end
#implmentation ...
- (void) testing {
Example *example = [[Example alloc] init];
NSString *testOne = [example testOne]; // does not work ; No visible #interface for 'Example' declares the selector 'testOne'
NSString *testTwo = [example testTwo]; // does work
}
#end
Obviously, by declaring class func it should require a different way to invoke it through Objective-C, but I have not seen any docs on this topic.
An example or help would be much appreciated.
Class methods are called on the class, not instances of the class:
NSString *testOne = [Example testOne];
NSString *testTwo = [Example testTwo];

explain how description method works in NSObject class

In NSObject.h header file, i have seen a method
+ (NSString *)description;
I knew that "+" sign before the method indicates that it is a class method
Here is my entire program
#import <Foundation/Foundation.h>
#interface A : NSObject
#end
#implementation A
#end
int main(int argc, const char * argv[])
{
#autoreleasepool {
A * aObj = [A new];
NSLog(#"%#",[A description]);//o/p: A
NSLog(#"%#",[aObj description]);//o/p: <A: 0x10010f5a0>
}
return 0;
}
I have used the description method with both class as well with instance of it and i got the o/p as well. why i have not received any error or warning while using a class method using an instance particularly in this case
[aObj description];//why this piece of code is working fine
Thank you in Advance
Because NSObject has two methods:
+ (NSString *)description; // Class method
- (NSString *)description; // Instance method
So when you do [A description] you're calling the class method (declared in NSObject Class), when you do [aObj description] you're calling the instance method (declared in NSObject Protocol).

Problems with subclasses inheriting class factory methods (Objective-C)

While I'm more than familiar with C#, I'm totally new at Objective C and iOS development. So I'm learning the language. What I don't understand is why the following code throws a compiler error (and yes, this is from the exercises at Programming with Objective C:
SNDPerson:
#interface SNDPerson : NSObject
#property NSString *first;
#property NSString *last;
+ (SNDPerson *)person;
#end
#implementation SNDPerson
+ (SNDPerson *)person
{
SNDPerson *retVal = [[self alloc] init];
retVal.first = #"Ari";
retVal.last = #"Roth";
return retVal;
}
#end
SNDShoutingPerson:
#import "SNDPerson.h"
#interface SNDShoutingPerson : SNDPerson
#end
#implementation SNDShoutingPerson
// Implementation is irrelevant here; all it does is override a method that prints a string
// in all caps. This works; I've tested it. However, if necessary I can provide more code.
// The goal here was for a concise repro.
#end
Main method:
- int main(int argc, const char * argv[])
{
SNDShoutingPerson *person = [[SNDShoutingPerson alloc] person]; // Error
...
}
The error is "No visible #interface for "SNDShoutingPerson" declares the selector "person".
Shouldn't this work? SNDShoutingPerson inherits from SNDPerson, so I would have assumed it got access to SNDPerson's class factory methods. Did I do something wrong here, or do I have to declare the method on SNDShoutingPerson's interface as well? The exercise text implies that what I did should Just Work.
Omit the +alloc when calling the class method:
SNDShoutingPerson *person = [SNDShoutingPerson person];
Briefly:
+ (id)foo denotes a class method. This takes the form:
[MONObject method];
- (id)foo denotes an instance method. This takes the form:
MONObject * object = ...; // << instance required
[object method];
Also, you can declare + (instancetype)person in this case, rather than + (SNDPerson *)person;.
change the line SNDShoutingPerson *person = [[SNDShoutingPerson alloc] person]; // Error
to
SNDShoutingPerson *person = [[SNDShoutingPerson alloc] init];
Cheers.
If you want to call class method:
SNDPerson person = [SNDPerson person];
person is a class method, but you're trying to call it with the incompletely constructed instance returned by alloc. Kill the alloc and just do [SNDShoutingPerson person].
This has nothing to do with subclasses, by the way. You would get the same error if you had written [[SNDPerson alloc] person].

Objective-C hidden static method calling

I google this question and spend some time to figure it out by myself but with a bad luck.
I need to call class's static method which is hidden for class's user.
// MyClass.h
#interface MyClass : NSObject
#end
// MyClass.m
#implementation MyClass
- (NSString *)myInstanceMethod
{
return #"result string";
}
+ (NSString *)myStaticMethod
{
return #"result string";
}
#end
------------------------------------------------------------
// MyCallerClass.m
#import "MyClass.h"
#implementation MyCallerClass
- (void) testMethod
{
MyClass *inst = [MyClass new];
// call 1
NSString *resultInstance = [inst performSelector:#selector(myInstanceMethod)];
// call 2
NSString *resultStaitc = [inst performSelector:#selector(myStaticMethod)];
// call
[MyClass myStaticMethod];
}
#end
Call 1 works good, Call 2 returns nil, Call 3 does not compile.
How can I call static method which does not defined in .h file and give correct returned object?
Thank in advance,
Rost
For Call 2 ,
since it is an class method you should call like
NSString *resultStaitc = [[inst class] performSelector:#selector(myStaticMethod)];
inst is the object.To call a class method you must call with class.
The object instance's class is supposed to be calling the method, not the instance itself.
For call 3
It should be working fine,The result value is never used .the compile error is because
+ (NSString *)myStaticMethod;
not declared in .h
use
NSString *resultStaitc1 =[MyClass myStaticMethod];
and it will return the value to the resultStaitc1
Another option is to declare an informal protocol for MyClass at the top of MyCallerClass.m. An informal protocol is just a category interface without the implementation block. You can stick you method declaration(s) in there. It does raise synchronisation problems between the two source files, but so does performSelector:. Doing it this way lets you call methods that have a different signature to just take [0-2] object arguments and return and object.

Accessing NSMutableData from another class

Ok I have declared the NSMutableData in the .h of class 1 as followed
NSMutableData *dataResponse;
#property (strong, nonatomic) NSMutableData *dataResponse;
in the .m of class 1 I have #synthezie dataResponse, and then I am giving it some data in a function.
I want to access dataResponse in class 2 with that data that I have assigned to it in the function.
How can I get the data from dataResponse in class 2? Any help would be great.
You can use helper class for accessing array in different class. Create an NSObject file in the project. I've named it Passing Class
In your PassingClass.h
#import <Foundation/Foundation.h>
#interface PassinClass : NSObject
{
NSMutableData *dataResponsetoPass;
}
+(PassinClass*)sharedString;
-(void)setdataResponsetoPass:(NSMutableData*)data;
-(NSMutableData*)getDataResponse;
In your PassinClass.m
#import "PassinClass.h"
#implementation PassinClass
#synthesize dataResponsetoPass;
static PassinClass*sharedString;
+(PassinClass*)sharedString
{
if(!sharedString)
{
sharedString=[[PassinClass alloc]init];
}
return sharedString;
}
-(void)setdataResponsetoPass:(NSMutableData*)data
{
dataResponsetoPass=data;
}
-(NSMutableData*)getDataResponse;
{
return dataResponsetoPass;
}
In your class1.h create instance of this helper class.
#import "PassinClass.h"
{
PassinClass*pClass;
}
In your class1.m, set the data using
pClass=[PassinClass sharedString];
[pClass setdataResponsetoPass:Your Data];
In your class2.m get the data using
pClass=[PassinClass sharedString];
[pClass getDataResponse];
NSLog the [pClass getDataResponse ] to check, if everything went well you should be able to pass the response data from class 1 to class 2.
Create an instance of the class and use the -mutableByes method. If you still need more information, check out the class reference for NSMutableData, right here