我目前正在学习一些目标C但仍然存在语法和创建对象的问题。
情况:我需要一个二维" personsArray"其中包含许多personArrays,每个personArrays包含一个NSString *名称和一个NSNumber * amount(double)。最后,我想在另一个视图中用数组数据计算一些东西,但我与此相距很远......
我的计划:在程序启动时创建NSMutable对象。如果我单击Button" Add Person",它会创建一个具有两个固定值的personArray(稍后它应该抓取textAreas的那些)。这是我的代码:
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject{
NSNumber *amount;
NSString *name;
}
- (void) createPersonArray:(double)theAmount withName:(NSString*) aName;
@end
Person.m
#import "Person.h"
@implementation Person
- (void) createPersonArray:(NSNumber*)theAmount withName:(NSString*) aName{
NSArray* personArray = [NSArray arrayWithObjects:theAmount,aName,nil];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet NSMutableArray *personsArray;
- (IBAction)addPerson:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "Person.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)addPerson:(id)sender {
Person *newPerson;
newPerson = [Person new];
[newPerson createPersonArray:100.00 withName:@"test"];
[_personsArray addObject:newPerson];
}
@end
我知道这不起作用,但我不知道如何实现我上面描述的内容。如果我构建程序,应用程序启动。如果我单击该按钮,调试器将跳转到createPersonArray并输出&#34;线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x40590000)和&#34;未使用的变量&#39; personArray&#39;&#34;。
我看过许多教程,但没有人解释过这种情况。我的代码必须如何构建才能实现我的目标?我是在正确的道路上还是错误的做法?
问候
答案 0 :(得分:0)
首先,personsArray变量不是出口。因此,请移除outlet
属性并将weak
替换为strong
:
@property (strong,nonatomic) NSMutableArray *personsArray;
其次,需要在使用之前初始化NSMutableArray,可能在viewDidLoad
中:
- (void)viewDidLoad
{
[super viewDidLoad];
_personsArray = [NSMutableArray new];
}
答案 1 :(得分:0)
我认为如果你想将数组保存到MutableArray,你只需要ViewController中的create方法。如果你创建Person.h,我建议你应该保存到MutableArray是Person Object。您可以使用以下代码:
Person.h:
@property (nonatomic, strong) NSNumber *amount;
@property (nonatomic, strong) NSString *name;
代替:
NSNumber *amount;
NSString *name;
Person.m:
@implementation Person
@synthesize name;
@synthesize amount;
- (void) createPersonArray:(double)theAmount withName:(NSString*) aName{
amount = [NSNumber numberWithDouble:theAmount];
name = aName;
}
ViewController.h
@property (strong,nonatomic) NSMutableArray *personsArray;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
_personsArray = [[NSMutableArray alloc]init];
}
- (IBAction)addPerson:(id)sender {
Person *newPerson;
newPerson = [Person new];
[newPerson createPersonArray:100.00 withName:@"test"];
[_personsArray addObject:newPerson];
}
如果你不明白。你可以在下面评论,我会帮助你。
当您需要包含时,您可以使用:
for (Person *person in _personsArray){
NSLog(@"Amount: %@ \n Name: %@ \n ",ps.amount,ps.name,);
}
或
for (int i = 0; i< [_personsArray count]; i++){
Person *ps = [_personsArray objectAtIndex:i];
NSLog(@"Amount: %@ - Name:%@",ps.amount,ps.name);
}
==================================更新============ ===============
double sumAmount = 0;
for (int i = 0; i < [_personsArray count]; i++){
Person *person = [_personsArray objectAtIndex:i];
sumAmount = sumAmount + [person.amount doubleValue];
}
double averageAmount = sumAmount / [_personsArray count];
NSLog(@"%f",averageAmount);