使用componentsSeparatedByString时,只能从“UIDeviceRGBColorSpace 1 1 1 1”获取值,sigabrt会发生吗?

时间:2011-11-15 10:51:09

标签: iphone ios xcode

我的字符串值是这个 //字符串应该类似于"UIDeviceRGBColorSpace 0.5 0 0.25 1"

我正在解析字符串以单独获取颜色值

 -(UIColor*)colorFromNSString:(NSString *)string {


        NSMutableArray *values = [string componentsSeparatedByString:@""];
        CGFloat red = [[values objectAtIndex:1] floatValue];
        CGFloat green = [[values objectAtIndex:2] floatValue];
       CGFloat blue = [[values objectAtIndex:3] floatValue];
      CGFloat alpha = [[values objectAtIndex:4] floatValue];
      UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
   //     NSLog(@"the color in string is %@",red);
       return color;
}

我在这一行中记录了sigabrt的错误消息

NSMutableArray *values = [string componentsSeparatedByString:@""];

崩溃日志

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM componentsSeparatedByString:]: unrecognized selector sent to instance 0x4b4a3e0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dcf5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f23313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dd10bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d40966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d40522 _CF_forwarding_prep_0 + 50
    5   PhotoApp                            0x00002800 -[PhotoAppViewController colorFromNSString:] + 67
    6   PhotoApp                            0x000027b5 -[PhotoAppViewController touchesBegan:withEvent:] + 950
    7   UIKit                               0x0038d0f4 forwardMethod2 + 92
    8   UIKit                               0x002e3d41 -[UIWindow _sendTouchesForEvent:] + 395
    9   UIKit                               0x002c4c37 -[UIApplication sendEvent:] + 447
    10  UIKit                               0x002c9f2e _UIApplicationHandleEvent + 7576
    11  GraphicsServices                    0x01727992 PurpleEventCallback + 1550
    12  CoreFoundation                      0x00db0944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    13  CoreFoundation                      0x00d10cf7 __CFRunLoopDoSource1 + 215

欢迎任何建议

2 个答案:

答案 0 :(得分:6)

 CGColorRef coloref = [selectst CGColor];
     const CGFloat* components = CGColorGetComponents(coloref);

    CGFloat red = components[0];
    CGFloat green = components[1];
    CGFloat blue = components[2];
    CGFloat alpha =1;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

希望这能帮到你

答案 1 :(得分:0)

-(UIColor*)colorFromNSString:(NSString *)string {

    NSArray *myarray = [string componentsSeparatedByString:@" "];

    NSLog(@"%@",[myarray objectAtIndex:1]);
    NSLog(@"%@",[myarray objectAtIndex:2]);
    NSLog(@"%@",[myarray objectAtIndex:3]);
    NSLog(@"%@",[myarray objectAtIndex:4]);
    CGFloat red = [[myarray objectAtIndex:1] floatValue];
    CGFloat green = [[myarray objectAtIndex:2] floatValue];
    CGFloat blue = [[myarray objectAtIndex:3] floatValue];
    CGFloat alpha = [[myarray objectAtIndex:4] floatValue];
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

    return color;
}