小编LE *_*ANG的帖子

无法检查NSDate何时为NULL

我在CoreData中创建一个名为"NoteEntity"的实体并生成NSManageObject子类

@interface NoteEntity : NSManagedObject
@property (nonatomic, retain) NSDate * time;
@end
Run Code Online (Sandbox Code Playgroud)

然后我addObserver检查选择对象时的时间变化:

[self addObserver:self forKeyPath:@"noteEntity.time" options:NSKeyValueObservingOptionOld context:KNoteEntityTime];
Run Code Online (Sandbox Code Playgroud)

观察员代码:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ( context == KNoteEntityTime ) {

        NSDate *oldTime = (NSDate *)[change objectForKey:NSKeyValueChangeOldKey] ;
        if (oldTime != NULL) {
            NSLog(@"CHANGE:From %@ - %@",oldTime,self.noteEntity.time);

        }

    }

    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要检查noteEntity.time是否从NULL更改但if(oldTime!= NULL)或if(oldTime)不是WORK,这里是日志

CHANGE:From <null> - (null)
CHANGE:From <null> - 2013-07-29 10:15:58 +0000
Run Code Online (Sandbox Code Playgroud)

请帮我看看我做错了什么.谢谢!

nsdate ios

2
推荐指数
1
解决办法
5496
查看次数

如何捕获Objective-C中的JSON错误?

按照这个教程:http://www.raywenderlich.com/5492/working-with-json-in-ios-5,我做了这样简单的应用程序:

#define kLatestKivaLoansURL [NSURL URLWithString: @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];
        NSError* error;
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                                                             options:kNilOptions
                                                               error:&error];
        NSArray* latestLoans = [json objectForKey:@"loans"];
        NSLog(@"Error: %@",error);
        NSLog(@"loans: %@",latestLoans); 

        dispatch_async(dispatch_get_main_queue(), ^(){
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            self.window.rootViewController = self.viewController;
            [self.window makeKeyAndVisible];
        });

    });

    return YES;
}

@end
Run Code Online (Sandbox Code Playgroud)

当网络不正常或JSON链接错误时,我得到相同的中断:" *因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'data parameter is nil"

如何捕获此错误?我只想显示一条Alert消息,而不是中断.

解析JSON数据时有多少种错误?

json objective-c ios

1
推荐指数
1
解决办法
1605
查看次数

如何同时实现两个UIButton?

我创建2个UIButton(不同的标签)并连接到1个动作但是当同时按下时,它会以小延迟触发2个动作.

- (IBAction)keysPress:(UIButton *)sender {
    UIButton *butOne = (UIButton *)[sender viewWithTag:0];
    UIButton *butTwo = (UIButton *)[sender viewWithTag:1];
    NSLog(@"BUT 1: %@ || BUT 2: %@",butOne, butTwo);
}
Run Code Online (Sandbox Code Playgroud)

记录总是2次:

2013-02-19 09:37:40.933 TestActions[1107:c07] BUT 1: <UIButtonLabel: 0xca4d450; frame = (65 67; 9 19); text = 'â'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xca4d4c0>> || BUT 2: <UIRoundedRectButton: 0xca4d310; frame = (161 164; 139 153); opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0xca4d270>> …
Run Code Online (Sandbox Code Playgroud)

uibutton ibaction ios

1
推荐指数
1
解决办法
143
查看次数

如果通过performSelectorInBackground控制UI会发生什么?

我读了一些UI接口只在MainThread上更新的信息.

我需要异步更新一些UIButtons,所以我使用performSelectorInBackground它,它在模拟器和设备(iPad4)上工作正常.

[self performSelectorInBackground:@selector(toggleToUpperWhenSingleShift) withObject:nil];

- (void)toggleToUpperWhenSingleShift{
    shiftStateIndicator = 1;
    for (UIView *aPad in self.subviews) {
        if ( [aPad isKindOfClass:[UIButton class]] ) {
            UIButton *aButPad = (UIButton *)aPad;
            NSMutableString *currentTitle = [NSMutableString stringWithString:[aButPad titleForState:UIControlStateNormal]];
            NSString *firstChar = [currentTitle substringToIndex:1];
            [currentTitle replaceCharactersInRange:NSMakeRange(0, 1) withString:[firstChar uppercaseString]];
            [aButPad setTitle:currentTitle forState:UIControlStateNormal];
            [aButPad setTitle:currentTitle forState:UIControlStateHighlighted];

            currentTitle = [NSMutableString stringWithString:[aButPad titleForState:UIControlStateSelected]];
            firstChar = [currentTitle substringToIndex:1];
            [currentTitle replaceCharactersInRange:NSMakeRange(0, 1) withString:[firstChar uppercaseString]];
            [aButPad setTitle:currentTitle forState:UIControlStateSelected];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我担心如果我保留我的代码会发生一些不需要的功能.谁能解释一下我的细节performSelectorInBackground

为什么不用它来更新UI以及为什么我的应用程序没问题?无论如何调试问题都会欣赏!

user-interface multithreading objective-c uiview ios

1
推荐指数
1
解决办法
955
查看次数

如何通过指针指定颜色名称来设置颜色?

正常,我经常通过以下方式设置背景颜色:aView.backgroundColor = [UIColor redColor];

现在我将所有颜色名称保存在NSArray中:

colorsArray = [[NSMutableArray alloc] initWithObjects:
               @"blackColor",
               @"darkGrayColor",
               @"lightGrayColor",
               @"whiteColor",
               @"grayColor",
               @"redColor",
               @"greenColor",
               @"blueColor",
               @"cyanColor",
               @"yellowColor",
               @"magentaColor",
               @"orangeColor",
               @"purpleColor",
               @"brownColor",
               nil];
Run Code Online (Sandbox Code Playgroud)

我想通过这样的NSString指针设置背景

NSInteger i = 0;
for (aView in viewArray) {
   NSString *colorName = [colorsArray objectAtIndex:i];
   aView.backgroundColor = color with colorName;//Can't find a method to set
   i++;
}
Run Code Online (Sandbox Code Playgroud)

我检查UIColor类,找不到任何方法来设置颜色与指向colorName的指针.请帮忙!还有别的办法吗?谢谢!

uicolor ios

0
推荐指数
1
解决办法
609
查看次数

如何使用框架和颜色自动绘制UIImage?

我试着创建一个框架和颜色的UIImage,但不知道正确的方法.

UIImage *aImage = [UIImage imageWithCIImage:[CIImage imageWithColor:[CIColor colorWithCGColor:[UIColor redColor].CGColor]]];
Run Code Online (Sandbox Code Playgroud)

以上行是否正确?如何创建图像大小?请帮忙!

core-image uiimage ios

0
推荐指数
1
解决办法
752
查看次数