小编foh*_*oho的帖子

奇怪的错误NSAssert

我弄清楚为什么会得到

use of undeclared identifier _cmd  did you mean rcmd
Run Code Online (Sandbox Code Playgroud)

在NSAssert的行上.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int x = 10;

    NSAssert(x > 11, @"x should be greater than %d", x);

    [pool drain];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

objective-c nsassert

46
推荐指数
2
解决办法
5320
查看次数

C - 参数化宏

我可以'弄清楚使用的优点是什么

#define CRANDOM() (random() / 2.33);
Run Code Online (Sandbox Code Playgroud)

代替

 float CRANDOM() {
    return random() / 2.33;
}
Run Code Online (Sandbox Code Playgroud)

c macros c-preprocessor

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

结构声明

我见过结构声明,看起来像这个

typedef struct br {
        int year;
        int km;

} Car;
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用那种结构

Car ford;
ford.year = 1980;
ford.km = 12
Run Code Online (Sandbox Code Playgroud)

但是对于宣言中的"br"是什么?

c

3
推荐指数
2
解决办法
667
查看次数

Objective-C中的指定初始值设定项

只是要确认一下,在Objective-C中创建指定初始值设定项的正确方法是什么?

这是启动ivars的正确方法吗?

你能建议我改进一下吗?

Person.h

@interface Person : NSObject 


@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, assign) NSString *sex;
@property (nonatomic, assign) int weight;

-(id)initWithName:(NSString *)name;
-(id)initWithName:(NSString *)name age:(int)age;
-(id)initWithName:(NSString *)name age:(int)age sex:(NSString *)s;
-(id)initWithName:(NSString *)name age:(int)age sex:(NSString *)s andWeight:(float)w;


@end
Run Code Online (Sandbox Code Playgroud)

Person.m

@implementation Person

@synthesize name = _name;
@synthesize sex = _sex;
@synthesize age = _age;
@synthesize weight = _weight;

-(id)initWithName:(NSString *)name age:(int)age sex:(NSString *)s andWeight:(float)w
{
    if(self = [super init])
    {
        [self setName:name];
        [self setAge:age]; …
Run Code Online (Sandbox Code Playgroud)

objective-c

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

Fadein/out文本动画

我正在使用NSTimer(5秒持续时间)和选择器从NSArray中随机选择名称.

这是一些代码

....

NSDate *endtime = [NSDate dateWithTimeIntervalSinceNow:5];

    [NSTimer scheduledTimerWithTimeInterval:0.2 
                                     target:self 
                                   selector:@selector(selectPlayer:) 
                                   userInfo:endtime 
                                    repeats:YES ];
Run Code Online (Sandbox Code Playgroud)

...

-(void)selectPlayer:(NSTimer *)timer
{
    if ([timer.userInfo timeIntervalSinceNow] < 0) 
    {
        [timer invalidate];
    }

    NSString *playerName = [[NSString alloc]initWithFormat:@"%@", 
                            [self.playersNames objectAtIndex:random() & [self.playersNames count]-1]];


    self.playersLabel.text = playerName;
    [playerName release]; 
}
Run Code Online (Sandbox Code Playgroud)

该代码完美地运作.self.playersLabel每隔0.2秒填充一个随机名称.

我想要的是添加一些fadein/fadeout动画效果,同时在playersLabel中更改名称.

怎么实现呢?

animation objective-c ios

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

应用程序崩溃在initWithStyle上

我无法理解我的应用程序崩溃的原因.这是代码:

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.tableContent = [NSArray arrayWithObjects:@"Blue", @"Yellow", @"Green", nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.tableContent count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    return [self.tableContent objectAtIndex:indexPath.row];

}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误

-[NSCFString setTableViewStyle:]: unrecognized selector sent to instance 0x4938
Run Code Online (Sandbox Code Playgroud)

objective-c uitableview ios

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

尝试为NSError设置本地化描述时代码崩溃

如果出现问题我正试图设置错误信息,但是我知道了

This class is not key value coding-compliant for the key NSLocalizedDescription
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码

-(id)getMoneyFromAccount:(int) sum error:(NSError **)error
{
    if(self.balance - sum < 0)
    {
        NSDictionary *details = [NSDictionary dictionary];
        [details setValue:@"You don't have enough money" forKey:NSLocalizedDescriptionKey];
        *error = [NSError errorWithDomain:@"money" code:200 userInfo:details];
        return nil;
    }
    self.balance = self.balance - sum;
    return  [NSNumber numberWithInt:self.balance];
}
Run Code Online (Sandbox Code Playgroud)

objective-c nserror ios

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

阵列枚举期间应用程序崩溃

这段代码有什么问题?我明白了

 Collection <NSCFArray: 0x101e1b6d0> was mutated while being enumerated
Run Code Online (Sandbox Code Playgroud)

它是NSMutableArray,而不是NSArray

NSMutableArray *set = [[NSMutableArray alloc]initWithObjects:@"first", @"second", @"third", @"third", nil];

    [set enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         if([obj isEqualToString:@"third"])
         {
             [set removeObjectAtIndex:idx];
         }
     }];

    [pool drain];
Run Code Online (Sandbox Code Playgroud)

enumeration objective-c ios

0
推荐指数
2
解决办法
1351
查看次数

验证文本字段长度

我正在尝试在textField中验证文本长度.这是我尝试的

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *textLenght = [NSString stringWithFormat:@"%@", [textField text]];
    if ([textLenght length] > 5)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" 
                                                message:@"Too long" 
                                                delegate:self 
                                                cancelButtonTitle:@"OK" 
                                               otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return NO;
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

这段代码效果很好.当我输入超过5个字符时,会显示警告框.问题是当我尝试删除文本字段的最后一个字符时,会再次显示警告框.

如何解决?

objective-c uitextfield uialertview ios

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