I'm working on an subclass of NSURLConnection. I added some properties and functionality. I implemented the NSURLConnectionDelegate and it's methods.
Now I need to pass the NSURLConnection (and it's properties) to my subclasses delegate. I implemented a delegate protocol for this.
Here is an code example:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if([_delegate respondsToSelector:_didReceiveDataSelector]) {
[_delegate performSelector:_didReceiveDataSelector withObject:connection];
}
Now I also need to return my subclasses properties. I tried to simply cast the instance:
mySubClass *obj = (mySubClass *)connection;
obj.userInfo = self.userInfo;
But it turned out (NSLog(#"%#", NSStringFromClass([obj class]));) that the cast returns an object which's class is still NSURLConnection.
Now I wonder how to merge all property values of the superclass instance and my subclasses properties.
Thanks for help!
Casting doesn't change an object's class. Casting just tells the compiler "This object is actually a whatever". It's for when the compiler is misunderstanding what class something is.
Given that casting doesn't seem to be working for you, then it means you're not being passed an instance of your custom subclass. So you should be looking at the code where you create the connection in the first place. I'd expect to see something like:
mySubClass *conn = [[mySubClass alloc] initWithRequest:req delegate:self];
Is that what your code looks like? If not (i.e. if you create an NSURLConnection rather than a mySubClass), switch to creating a mySubClass.
I solved the problem.
Now I'm doing this:
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate {
self = [super initWithRequest:request delegate:self];
if(self) {
_delegate = delegate;
}
}
This way I'm able to receive the delegate methods on my subclass, extend the informations in the method and end the custom selectors to the delegate.
In the viewControllers I can do something like this:
mySubClass *con = [[mySubClass alloc] initWithRequest:req delegate:self];
[con setDidReceiveResponseSelector:#selector(connection:didGetResponse)];
[con start];
So I can have like ten URLRequests within the same viewController with different delegate methods for the same action.
Related
I am from Actionscript Background. In Actionscript Class Method can access only Class Methods and Class properties.
But In Objective C,
How Class method gameResultAll can access Instance Method initFromPlist
+(NSMutableArray *)gameResultAll://Class Method
-(id)initFromPlist:(id)plist;//Instance Method
NSMutableArray *gameResults = [GameResult gameResultAll]; // (returns GameResult array)
Why [self init] method is called instead of [super init] to create an instance from class method.
Thanks in advance.
#import "GameResult.h"
#implementation GameResult
#define GAME_RESULT_KEY #"gameresult_key"
#define SCORE_KEY #"score"
+(NSMutableArray *)gameResultAll
{
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for (id plist in [[[[NSUserDefaults standardUserDefaults] dictionaryForKey:GAME_RESULT_KEY] mutableCopy] allValues])
{
GameResult *gameResult = [[GameResult alloc] initFromPlist:plist];
[resultArray addObject:gameResult];
}
return resultArray;
}
//Designated initialiser
-(id)initFromPlist:(id)plist
{
self = [self init];
if(self)
{
if([plist isKindOfClass:[NSDictionary class]])
{
NSDictionary *resultDictionary = (NSDictionary*)plist;
_score = (int)resultDictionary[SCORE_KEY];
}
}
return self;
}
You asked:
How Class method gameResultAll can access Instance Method initFromPlist
It can access that method because you used the alloc method, which creates an instance of GameResult. Now that you have an instance, you can use instance methods in conjunction with this instance.
By the way, this is a very common pattern, a "convenience" class method that allocates an instance of an object (with alloc) and initializes the object (with init or some permutation of that). Or, as in this case, it can create an array of these objects.
You then go on to ask:
Why [self init] method is called instead of [super init] to create an instance from class method.
I can understand the confusion, but there is an important, yet subtle distinction in the behavior of these two.
Imagine this scenario:
At some future date, you subclass GameResult, e.g. ArcadeGameResult;
You implemented an init method for ArcadeGameResult that initializes some properties unique to this subclass; and
You happen to initialize a ArcadeGameResult instance like so:
ArcadeGameResult *agr = [[ArcadeGameResult alloc] initFromPlist:plist];
Because the initFromPlist uses [self init], it means that the the initFromPlist method of GameResult will end up calling the init method of the object (which in this example, is actually a ArcadeGameResult object). But if initFromPlist in GameResult called [super init] instead, it would not have called ArcadeGameResult's init method and thus initFromPlist would be problematic if ever used in conjunction with a subclass.
Bottom line, unless the method you're calling is the exact same method signature, it's safer to call the self rendition rather than the super rendition. It's a little more flexible in case you ever decide to subclass in the future.
There is a corollary to the counsel. When calling class methods from an instance method, you should refer to [self class] rather than the class name. So, imagine your GameResult class had a class method:
+ (void)someClassMethod
{
// do something
}
If you had some GameResult instance method that was going to avail itself of this method, you might be tempted to write:
- (void)someInstanceMethod
{
// do some stuff
[GameResult someClassMethod];
}
But that's not a good idea. You would instead use the following:
- (void)someInstanceMethod
{
// do some stuff
[[self class] someClassMethod];
}
They look very similar, but the latter lets you implement a someClassMethod in a subclass, and this instance method will still work. If you use the former construct, the subclassed class method wouldn't be called by someInstanceMethod.
These are subtle issues, and probably not critical for your current code sample. But hopefully it illuminates the choice of [self init] versus [super init] in this situation.
In Actionscript Class Method can access only Class Methods and Class properties.
That's not different in Objective-C either (because nothing else would make sense), so:
How Class method GameResultAll can access Instance Method initFromPlist
Only through a valid instance.
Why [self init] method is called instead of [self super] to create an instance from class method.
Because the latter is a syntax error, perhaps? Read a basic Objective-C tutorial.
I'm quite a newbie in Objective C, though I have some background in Java reflection.
Here, I have a classic class method findAll that find all the domain objects from the database. The class Univers directly inherits from DomainObject
#interface DomainObject : NSObject
- (NSString *) execute : (NSString*) method withJson:(NSString*)json;
+ (NSString*)findAll: (NSString*)json;
#end
#implementation DomainObject
- (NSString *) execute: (NSString*) method withJson:(NSString*)json{
method = [NSString stringWithFormat:#"%#%#", method, #":"];
//method is 'findAll:'
NSString* result = [ self performSelector:
NSSelectorFromString(method) withObject:json];// Error here
return result;
}
#end
The code was working when findAll was NOT a class method (ie -findAll declaration), but now I have the error : NSInvalidArgumentException -[Univers findAll:]
It clearly seems that the runtime is looking for an instance method.
Any idea to find my class method ?
Instead of calling
NSString* result = [self performSelector:NSSelectorFromString(method) withObject:json];
you need to call
NSString* result = [[self class] performSelector:NSSelectorFromString(method) withObject:json];
for class methods.
After all it's the object instance's class that supposed to be calling the method, not the instance itself.
Short explanation: NSObject implements - (Class)class; (not to be mistaken with + (Class)class of similar effect, which NSObject implements, too!) which returns the Class object of your instance object. Keep in mind that in Objective-C in addition to plain instance objects, Classes are actual objects, too: objects of type Class, that is (vs. id, NSObject, …).
See the documentation for the -class method here.
Btw, you should probably wrap your method call into an conditional block to prevent exceptions caused by calls to missing methods.
SEL selector = NSSelectorFromString(method);
if ([[self class] respondsToSelector:selector]) {
NSString* result = [[self class] performSelector:selector withObject:json];
}
In general it's a common pattern in Objective-C to call an object's class method by receiving the class object via [object class].
Consider this case of a class called Foo implementing a convenience method for returning an autporeleased instance of itself (to be called via: Foo *newFoo = [Foo foo];):
While it would certainly be possible to implement said method like this (after all we know the object's class name, right?):
+ (id)foo {
return [[[Foo alloc] init] autorelease];
}
the correct way is this:
+ (id)foo {
return [[[self alloc] init] autorelease];
}
As the first one would cause problems with polymorphism in subclasses (Such as a subclass called FooBar, for which it should clearly be [FooBar alloc] …, not [Foo alloc] …. Luckily [[self class] alloc] solves this dynamically).
While this is clearly not the right place for a thorough explanation of this (rather offtopic one might say) it's certainly worth noting/warning about, imho.
I am trying to override a UIStoryboard method using a category. Here is my implementation:
#import "UIStoryboard+SomeCategory.h"
#import <UIKit/UIKit.h>
#implementation UIStoryboard(SomeCategory)
-(id)instantiateInitialViewController
{
NSLog(#"SUPER CLASS: %#", [super class]); // logs "UIStoryboard"
NSLog(#"SUPER RTS : %#", [super respondsToSelector:#selector(instantiateInitialViewController)] ? #"YES" : #"NO"); // logs "YES"
return [super instantiateInitialViewController];
}
#end
when I add:
UIViewController *viewController = [super instantiateInitialViewController]
Why do I get the compiler error:
Receiver type 'NSObject' for instance message does not declare a method with selector 'instantiateViewController'
If you use super when overriding methods using a category, the method will be called on the superclass of the object, not the object you are overriding the method on. You haven't made a subclass of UIStoryboard, so super refers to NSObject - which is reflected accurately in your error message.
I don't know what's going on with your log messages, though.
Using a category to override a method means that you can't call the original method. You'll need to either make a subclass of UIStoryboard or an entirely new method in the category, that calls [self instantiateInitialViewController].
You should note that [super class] is not the same as [self superclass]. Quoting the docs:
Objective-C provides two terms that can be used within a method definition to refer to the object that performs the method—self and super.
They differ in how the compiler will search for the method implementation, and in some cases they will mean just the same.
In this case you want:
NSLog(#"SUPER CLASS: %#", [self superclass]); // logs "NSObject"
to check an object's super class class, and you'll need a UIStoryBoard subclass, not a category, to be able to use:
return [super instantiateInitialViewController];
Why [super class] doesn't log what you expect is another subject. If you're interested, this post What is a meta-class in Objective-C? is a good starting point.
You need to use method swizzling. good explanation of how to use it for your purposes here: http://b2cloud.com.au/how-to-guides/method-swizzling-to-override-in-a-category
If you really want to call that method from a UIViewController, your category should be:
#implementation UIViewController(SomeCategory)
Even so, it would call the super of your UIViewController, so it would still not work. You also need to do the following:
UIViewController *viewController = [self instantiateInitialViewController]
I have a class object that i'm using a lot from different places.
Now i'm using the class like this:
myClass.delegate = self;
[myClass doSomething];
doSomething creates a new class object that calculate stuff and can take up to 1 min before it sends back a result to the delegate like this:
-(void)doSomething {
CalculateStuff *calc = [[calculateStuff alloc] init];
calc.delegate = self;
[calc calculate];
}
/* Calculate Delegate */
-(void)didCalculate {
[[self delegate] didDoSomething];
}
Problem is that i from another place is calling the same thing it will call my latest delegate and this causes a lot of problems.
Question:
Is there a way to send the delegate as an object without having to set it as the property?
I've written it like this and Xcode give me warnings "Incomplete implementation of MyClass"
[myClass doSomethingWithDelegate:self];
And
-(void)doSomethingWithDelegate:(id)delegate {
CalculateStuff *calc = [[calculateStuff alloc] init];
[calc calculateWithDelegate:delegate];
}
/* Calculate Delegate */
-(void)didCalculateWithDelegate:(id)delegate {
[delegate didDoSomething];
}
EDIT
Just tried it out and it seems to work, but how can i get rid of the warnings in Xcode?
I think your problem is that you forgot to remove the declaration of doSomething and didCalculate from your class's interface (or class extension).
Ideally you should create a protocol to ensure that your delegate has the required method(s). For instance:
#protocol DoSomethingDelegate <NSObject>
- (void)didDoSomething;
#end
Then use id<DoSomethingDelegate> instead of just id.
Passing a block would also be a valid solution to this problem, although it's a bit trickier.
As a part of my transition process from C++ to Objective-C, I intensively read book Cocoa and Objective C Up and Running.
In one of the book code examples, there is a line that does not make sense to me with my current level of knowledge:
It is a declaration of class method + (Photo*) photo;.
Could anybody explain me the reason, please, why the author had decided for the method (Photo*) photo; to declare it as a class method instead of instance method?
I have studiet the theory, that the instane method is something like a class member function and that class method is something like static function in C++. But this still does not answer my question.
Here is the declaration code:
#import <Foundation/Foundation.h>
#interface Photo : NSObject{
NSString* caption;
NSString* photographer;
}
+ (Photo*) photo;
- (NSString*) caption;
- (NSString*) photographer;
- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;
#end
The implementation code follows:
#import "Photo.h"
#implementation Photo
- (id)init
{
self = [super init];
if (self) {
[self setCaption:#"Default Caption"];
[self setPhotographer:#"Default Photographer"];
}
return self;
}
+ (Photo*) photo {
Photo* newPhoto = [[Photo alloc] init];
return [newPhoto autorelease];
}
- (NSString*) caption {
return caption;
}
- (NSString*) photographer {
return photographer;
}
- (void) setCaption:(NSString *)input {
[caption autorelease];
caption = [input retain];
}
- (void) setPhotographer: (NSString *)input {
[photographer autorelease];
photographer = [input retain];
}
- (void)dealloc
{
[self setCaption:nil];
[self setPhotographer:nil];
[super dealloc];
}
#end
The + (Photo*) photo method is a Factory Method that encapsulates the details of creating an object of the Photo class.
A Factory Method enforces
encapsulation, and allows an object to
be requested without inextricable
coupling to the act of creation.
In this particular example the information being hidden by the factory method is memory management, since the client does not need to worry about releasing the returned object.
It is a common practice in Objective-C APIs to provide factory methods in classes that return autoreleased objects of that same classes. These methods must not contain any of the words “alloc”, “new”, “copy”, or “mutableCopy”, which, according to the convention, indicates that the caller doesn't own the returned object, i.e. it doesn't have to be explicitly released.
Related resources:
Memory Management Rules
Meta answer:
One issue; that method should be
declared as returning id and should
return [[[self alloc] init]
autorelease]; (one line or two,
doesn't matter -- just should refer to
the Class directly). As it is, Photo
is gonna be a pain to subclass.
Expanding -- given this:
+ (Photo*) photo {
Photo* newPhoto = [[Photo alloc] init];
return [newPhoto autorelease];
}
If the class were subclassed, this factory method would not work without being overridden to do pretty much the same thing. However, since Objective-C doesn't support co-variance and contra-variance, there would be no way to declare the subclass's implementation of +photo to return an instance of the subclass without also running a significant risk of compiler warnings. Alternatively, you could down-cast the return value to the more specific class, but that is rife with fragility.
Instead, do this:
+ (id) photo {
id newPhoto = [[self alloc] init];
return [newPhoto autorelease];
}
This fixes both issues:
since it uses self, it'll instantiate an instance of whatever class it is implemented on, including subclasses of Photo.
since it returns id, callers can do both of the following without issue:
Photo *p = [Photo photo];
SubclassOfPhoto *s = [SubclassOfPhoto photo];
In this scenario, photo is a convenience method, which returns you an autoreleased instance of the class.
Since the purpose of photo is to give you an instance, it wouldn't make sense to make it an instance method which would require you to already have an instance.
If you're familiar with Factory Methods, the photo method is similar to that.
+photo is like a constructor. You need a way to get an object to send instance methods to, and this gives you an autoreleased one.
It is equivalent to a static method, as you say. In this case (and all cases of [ClassName className] methods) it's basically a factory method. You're asking the class to construct an instance of itself and pass it back. All such methods should return an autoreleased object.
You can safely ignore methods like that if you want - there will usually be an alloc+init equivalent, but it's often more convenient to use the class method, especially if you're creating a throaway object and don't want to retain it.
Finally, you'll sometimes find classes which require you to use the class method, as they'll hide some clever logic wherein an instance of another class is actually returned. You'll sometimes hear these described as 'class clusters'.
Could anybody explain me the reason, please, why the author had decided for the method (Photo*) photo; to declare it as a class method instead of instance method?
It's basically a wrapper of the constructor litany. Note the source:
+ (Photo*) photo {
Photo* newPhoto = [[Photo alloc] init];
return [newPhoto autorelease];
}
Allocate a new Photo, initialize it, mark it autorelease, and return it. Since it creates the object, there is no object yet to operate upon, ergo this needs to be a class method.