黑马程序员技术交流社区

标题: 这个问题怎么解决? [打印本页]

作者: 冰点    时间: 2014-11-23 11:08
标题: 这个问题怎么解决?
  1. #import "Person.h"

  2. int main(){


  3.         Person *stu1=[[Person alloc]initWithName:@"liyi" age:21 score:24];
  4.         Person *stu2=[[Person alloc]initWithName:@"liyi" age:31 score:24];
  5.         Person *stu3=[[Person alloc]initWithName:@"liyi" age:1 score:24];
  6.         Person *stu4=[[Person alloc]initWithName:@"liyi" age:61 score:24];
  7.         Person *stu5=[[Person alloc]initWithName:@"liyi" age:41 score:24];
  8.         int i=0,j;
  9.         BOOL flag=YES;
  10.        
  11.         Person *temp=nil;
  12.         NSArray *stuArray = [NSArray arrayWithObjects:stu1, stu2,stu3,stu4,stu5, nil];
  13.         int count=[stuArray count];
  14.         for(i=count-1;i>=0;i++){
  15.                 NSLog([stuArray objectAtIndex:i] );
  16.         }
  17. return 0;
  18. }
复制代码

Person.h
  1. #import <Foundation/Foundation.h>
  2. @interface Person :NSObject
  3. {
  4.         @private NSString *_name;
  5.         @private int _age;
  6.         @private int _score;
  7. }
  8. -(void) setName:(NSString *)name;
  9. -(NSString *) name;
  10. -(void) setAge:(int) age;
  11. -(int) age;
  12. -(void) setScore:(int) score;
  13. -(int) score;
  14. - (id) initWithName:(NSString *)name age:(int)age score:(int)score;
  15. @end
复制代码

Person.m
  1. #import "Person.h"
  2. @implementation Person

  3. -(void) setName:(NSString *)name{
  4.        
  5.                 _name=name;
  6.        
  7. }
  8. -(NSString *)  name{
  9.         return _name;
  10. }
  11. -(void) setAge:(int) age{
  12.         _age=age;
  13. }
  14. -(int) age{
  15.         return _age;
  16. }
  17. -(void) setScore:(int) score{
  18.         _score=score;
  19. }
  20. -(int) score{
  21.         return _score;
  22. }


  23. - (id) initWithName:(NSString *)name age:(int)age score:(int)score{
  24.         if(self=[super init]){
  25.                 _name=name;
  26.                 _age=age;
  27.                 _score=score;
  28.         }
  29.         return self;
  30. }

  31. - (NSString *)description{
  32.         return [NSString stringWithFormat:@"My Name Is %@  Age Is %d Score Is %d",_name,_age,_score];
  33. }
  34. @end
复制代码


运行的时候报错。。。


QQ图片20141123110311.jpg (52.86 KB, 下载次数: 21)

QQ图片20141123110311.jpg

作者: Jr_711    时间: 2014-11-23 13:24
把main函数里的代码放到@autoreleasepool{}里试试
作者: 冰点    时间: 2014-11-23 14:06
Jr_711 发表于 2014-11-23 13:24
把main函数里的代码放到@autoreleasepool{}里试试

@autoreleasepool{}好像是编译器特性,我没有使用Xcode,好像不能用这个。。。
作者: 从今以后    时间: 2014-11-23 19:52
for(i=count-1;i>=0;i++){
                NSLog([stuArray objectAtIndex:i] );
这应该是i--吧(⊙o⊙),NSLog我没见过这种用法- -!。。NSLog(@"%@", [stuArray objectAtIndex:i]);
如果你是想打印Person5个对象的话>_<
作者: 冰点    时间: 2014-11-24 18:15
从今以后 发表于 2014-11-23 19:52
for(i=count-1;i>=0;i++){
                NSLog([stuArray objectAtIndex:i] );
这应该是i--吧(⊙o⊙),N ...

:Q,是啊




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2