[英]iOS description method not called
I've overridden the description method of an object I created, quite simple. 我已经覆盖了我创建的对象的描述方法,非常简单。 This object is a subclass of
NSMutableURLRequest
. 该对象是
NSMutableURLRequest
的子类。
- (NSString *)description
{
return [[NSString alloc] initWithData:self.HTTPBody encoding:NSUTF8StringEncoding];
}
I also put - (NSString *)description;
我还放了
- (NSString *)description;
in the .h 在.h中
But it is not called when I NSLog
the object. 但是当我
NSLog
对象时没有调用它。 It is not a NSManagedObject
. 它不是
NSManagedObject
。 Even the debugger won't step into "description" if I only call myObject.description;
如果我只调用
myObject.description;
即使调试器也不会进入“ description” myObject.description;
. 。 I am calling the method precisely on an instance of my object, not just a
NSMutableURLRequest
. 我正在精确地在对象的实例上调用该方法,而不仅仅是
NSMutableURLRequest
。
EDIT: I instantiate the object like this: 编辑:我实例化对象是这样的:
MYRequest *myRequest = [MYRequest requestWithFilter:myFilter];
NSLog(@"%@", myRequest);
And here is the factory method: 这是工厂方法:
@interface MYRequest : NSMutableURLRequest
+ (instancetype)requestWithFilter:(NSString *)filter;
@implementation MYRequest
+ (instancetype)requestWithFilter:(NSString *)filter
{
// some config
MYRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
// some more config
return request;
}
What the heck? 有没有搞错?
您没有子类的实例,只有一个普通的NSMutableURLRequest
。
NSMutableURLRequest
or NSURLRequest
classes doesn't have - (NSString *)description
method declared. NSMutableURLRequest
或NSURLRequest
类没有声明- (NSString *)description
方法。
You are accessing NSObject
's method - (NSString *)description
. 您正在访问
NSObject
的方法- (NSString *)description
。
You should declare your own description
method in your custom class and call it like: 您应该在自定义类中声明自己的
description
方法,并按如下方式调用它:
NSString *myObjectDescription = [myCustomObject description];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.