[英]NSString+Extension warning when creating a NSMutableString from self
in an iphone application I created and extension class to the NSString. 在我创建的iphone应用程序和NSString的扩展类中。
NSString+Extensions.m
class NSString+Extensions.m
类
In one of the methods I need to convert the string to NSMutableString. 在其中一个方法中,我需要将字符串转换为NSMutableString。
I tried to use this: 我试着用这个:
NSMutableString * stringToManipulate = [NSMutableString stringWithString:self];
But it is giving a warning: 但它正在发出警告:
Incompatible pointer types sending 'const Class' to parameter of type 'NSString *'
To my knowlede self is a reference to the string I called the method on, right? 对我的知识自我是对我称之为方法的字符串的引用,对吧? so why shouldn't it be of type NSString*?
那么为什么它不应该是NSString *类型? knowing that I can use the usual NSString methods on self.
知道我可以在自己上使用通常的NSString方法。
Any idea on this issue? 关于这个问题的任何想法?
Thanks 谢谢
Check whether the method is declared as a class method (with the + symbol) or an instance method (with the - symbol). 检查方法是声明为类方法(使用+符号)还是实例方法(使用 - 符号)。 If its the first case, the compiler warning is normal.
如果是第一种情况,编译器警告是正常的。 Turn the method to an instance one then
然后将方法转换为实例1
In Objective-C, when you call self
within a class method, it references the class object. 在Objective-C中,当您在类方法中调用
self
时,它会引用类对象。 For an NSString
class method, calling self
is the same as calling [NSString class]. 对于
NSString
类方法,调用self
与调用[NSString类]相同。
So, your line is the same as : 所以,你的行与以下相同:
NSMutableString * stringToManipulate = [NSMutableString stringWithString:[self class]];
If you change your method from a class one (+ symbol) to an object one (- symbol), it will work but you must call it differently (from an NSString
object). 如果将方法从第一类(+符号)更改为一个对象( - 符号),它将起作用但您必须以不同方式(从
NSString
对象)调用它。
If you want further explanations, can you post the entire method declaration ? 如果您想进一步解释,可以发布整个方法声明吗? You can also check the NSObject protocol reference for more informations on the +class method : https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
您还可以查看NSObject协议参考以获取有关+类方法的更多信息: https : //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
Hope this will help, 希望这会有所帮助,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.