A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冰点 中级黑马   /  2014-11-23 11:08  /  1125 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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

评分

参与人数 1黑马币 +1 收起 理由
星河鹭起 + 1

查看全部评分

4 个回复

倒序浏览
把main函数里的代码放到@autoreleasepool{}里试试
回复 使用道具 举报
Jr_711 发表于 2014-11-23 13:24
把main函数里的代码放到@autoreleasepool{}里试试

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

:Q,是啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马