【问题标题】:Integer to CGFloat help needed整数到 CGFloat 需要帮助
【发布时间】:2010-09-16 23:46:35
【问题描述】:

在我的 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.

【问题讨论】:

    标签: iphone integer cgfloat


    【解决方案1】:

    你应该保留 appColor 并在你的 dealloc 方法中释放它。你最有可能取消引用一个错误的指针

    Memory Management Programming Guide 可以作为一个很好的参考

    【讨论】:

    • 非常感谢。那成功了。我在 CGFLoat 上苦苦挣扎,我忽略了这样一个基本的东西。
    • 我也对错误信息中的“-[__NSCFArray CGColor]:”感到很困惑,但我猜我不应该注意。
    • @Sjakelien 这样的内存错误是出了名的难以追踪。找到它们的一种有用方法是在启用僵尸的情况下运行。
    • 有趣:我启用了僵尸,代码运行良好,没有保留。是它的作用吗?这样不是很危险吗?只是为了检查内存管理是否有问题,还是实际上修复了一些问题?这对于像我这样邋遢的人来说真的很棒......
    • 或者我只是看到了一些东西。我要睡觉了。
    【解决方案2】:

    为我工作;)

    #import <UIKit/UIKit.h>
    
    @interface deleteColorAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        UINavigationController *navigationController;
        UIColor *appColor;
    }
    
    @property (nonatomic, retain) UIColor *appColor;
    
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    
    @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
    
    -(UIColor*)readAppSettings;
    
    @end
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        // Override point for customization after application launch.
        // Add the navigation controller's view to the window and display.
    
        [self readAppSettings];
        UILabel *label = [[UILabel alloc] init];
        label.textColor = appColor;
        label.text = @"This is a test";
        label.frame = CGRectMake(100, 100, 100, 40);
    
        [navigationController.view addSubview:label];
        [window addSubview:navigationController.view];
        [window makeKeyAndVisible];
        [label release];
        return YES;
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
    
        // Save data if appropriate.
    
    
    }
    
    -(UIColor*)readAppSettings
    {
        NSString *path = [[NSBundle mainBundle] bundlePath];
        NSString *finalPath = [path stringByAppendingPathComponent:@"appSettings.plist"];
        NSDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
    
        float RedComponent = [[plistDictionary objectForKey:@"Red"]floatValue] / 255.0f ;
        float GreenComponent = [[plistDictionary objectForKey:@"Green"]floatValue] / 255.0f ;
        float BlueComponent = [[plistDictionary objectForKey:@"Blue"]floatValue] / 255.0f ;
    
        appColor = [UIColor colorWithRed:   RedComponent  
                                   green:   GreenComponent 
                                    blue:   BlueComponent 
                                   alpha:   1.0f];
        return [appColor retain];
    }
    
    
    
    - (void)dealloc {
        [appColor release];
        [window release];
        [navigationController release];
        [super dealloc];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多