整数到 CGFloat 需要帮助
在我的 iPhone 应用程序中,我有一个 appSettings.plist。这允许我和其他人简单地更改一些参数。参数之一是应用程序的主要颜色。 .plist 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Red</key>
<integer>255</integer>
<key>Green</key>
<integer>123</integer>
<key>Blue</key>
<integer>124</integer>
<key>compositeRGB</key>
</dict>
</plist>
在我的代码中,我读取此文件,并尝试用这三个数字创建 UIColor。我不得不承认我对 CGFLoats 不太了解,我怀疑这就是我遇到麻烦的原因。这就是我所做的:
-(void)readAppSettings
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"appSettings.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
unsigned int RedComponent = [[plistDictionary objectForKey:@"Red"]intValue];
unsigned int GreenComponent = [[plistDictionary objectForKey:@"Green"]intValue];
unsigned int BlueComponent = [[plistDictionary objectForKey:@"Blue"]intValue];
appColor = [UIColor colorWithRed: ((float) RedComponent / 255.0f)
green: ((float) GreenComponent / 255.0f)
blue:((float) BlueComponent / 255.0f)
alpha:1.0f];
}
每当我尝试使用 appColor
作为 UIColor 时,我的应用程序就会崩溃,并出现以下错误:
'-[__NSCFArray CGColor]: 无法识别的选择器发送到实例 0x7b0ab20'
Could somebody explain to me what I'm doing wrong. You don't have to be polite.
In my iPhone app, I have a appSettings.plist. This allows me, but also others to simply change some parameters. One of the parameters is the predominant color of the app. The .plist looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Red</key>
<integer>255</integer>
<key>Green</key>
<integer>123</integer>
<key>Blue</key>
<integer>124</integer>
<key>compositeRGB</key>
</dict>
</plist>
In my code, I read this file, and try to make a UIColor out of these three numbers. I have to admit that I don't know too much about CGFLoats, and I suspect that that is the cause of my trouble. This is what I do:
-(void)readAppSettings
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"appSettings.plist"];
NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
unsigned int RedComponent = [[plistDictionary objectForKey:@"Red"]intValue];
unsigned int GreenComponent = [[plistDictionary objectForKey:@"Green"]intValue];
unsigned int BlueComponent = [[plistDictionary objectForKey:@"Blue"]intValue];
appColor = [UIColor colorWithRed: ((float) RedComponent / 255.0f)
green: ((float) GreenComponent / 255.0f)
blue:((float) BlueComponent / 255.0f)
alpha:1.0f];
}
whenever I try to use appColor
as a UIColor, my app crashes, with the following error:
'-[__NSCFArray CGColor]: unrecognized selector sent to instance 0x7b0ab20'
Could somebody explain to me what I'm doing wrong. You don't have to be polite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该保留 appColor 并在 dealloc 方法中释放它。您很可能正在取消引用错误的指针
内存管理编程指南可以作为很好的参考
you should retain appColor and release it in your dealloc method. You're dereferencing a bad pointer most likely
The Memory Management Programming Guide can be a good reference
对我有用;)
Works for me ;)