我有一个定义的类别:
#import <Foundation/Foundation.h>
@interface NSString (MyApp)
+ (UIColor *)colorFromHexString;
@end
#import "NSString+MyApp.h"
@implementation NSString (MyApp)
+ (UIColor *)colorFromHexString
{
self = [self stringByReplacingOccurrencesOfString:@"#" withString:@""];
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner scanHexInt:&rgbValue];
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
@end
我在行self = [self stringByReplacingOccurrencesOfString:@"#" withString:@""];
上收到错误消息:"No know class method for selector stringByReplacingOccurrencesOfString:withString" and "cannot assign self in a class method"
。
我很困惑为什么我无法在NSString Category
中执行此操作。我看到examples online与NSString Category
中的'self'非常相似,所以我不确定它为什么不在这里工作。
任何人都知道我做错了什么?
答案 0 :(得分:1)
您的代码中存在多个错误。
使用实例方法( - )代替静态方法(+)
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady(){
player = new YT.Player('MyVideoID',{
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event){
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
var slideTimeMS = 10000; /* This is a variable which exists in my external file which I would like the player state to manipulate
}
}
同样在第1行分配// INCORRECT. self in a static method points to class object
// + (UIColor *)colorFromHexString
// CORRECT. self is instance method points to instance of class.
- (UIColor *)colorFromHexString
而不是hexString
。
self