我弄清楚为什么会得到
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) 我可以'弄清楚使用的优点是什么
#define CRANDOM() (random() / 2.33);
Run Code Online (Sandbox Code Playgroud)
代替
float CRANDOM() {
return random() / 2.33;
}
Run Code Online (Sandbox Code Playgroud) 我见过结构声明,看起来像这个
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"是什么?
只是要确认一下,在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) 我正在使用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中更改名称.
怎么实现呢?
我无法理解我的应用程序崩溃的原因.这是代码:
-(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) 如果出现问题我正试图设置错误信息,但是我知道了
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) 这段代码有什么问题?我明白了
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) 我正在尝试在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 ×7
ios ×5
c ×2
animation ×1
enumeration ×1
macros ×1
nsassert ×1
nserror ×1
uialertview ×1
uitableview ×1
uitextfield ×1