2012-07-12 48 views
0

我正在iOS SDK中创建一个程序,其中有一组按钮。当单击按钮时,其标题将被添加到数组中,并且该数组将显示在已分配的标签中。XCode中的数组管理帮助?

当我尝试创建删除和清除按钮时,显示错误消息。它们出现在function_builder = function_builder.removeLastObject;和function_builder = function_builder.removeAllObjects; .m文件的行。错误消息是相同的:从不兼容的类型'void'分配'NSMutableArray * _strong'。我该如何解决? 谢谢你的任何和所有帮助

这里是.h文件:

#import <UIKit/UIKit.h> 

@interface SecondViewController : UIViewController 
@property (nonatomic,strong) IBOutlet UILabel *equation_field; 
@property (nonatomic) NSMutableArray *function_builder;//declare array// 
@property(nonatomic, readonly, retain) NSString *currentTitle;//declare button titles// 
@end 

这里是.m文件:

#import "SecondViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 
@synthesize equation_field; 
@synthesize currentTitle; 
@synthesize function_builder; 
NSMutableArray *function_builder;//create the array name// 

- (IBAction)functionButtonPress:(UIButton *)sender {//code for all buttons except delete and clear// 
[function_builder addObject: sender.currentTitle];//when button is pressed, its title is added to the array// 
self.equation_field.text = function_builder.description;//the contents of the array appear in the assigned label// 
} 
- (IBAction)delete:(UIButton *)sender {//create delete button// 
function_builder = function_builder.removeLastObject; //ERROR OCCURRING HERE: Assigning to 'NSMutableArray *_strong' from incompatible type 'void'// 
} 

- (IBAction)clear:(UIButton *)sender{//create clear button// 
function_builder = function_builder.removeAllObjects;//ERROR OCCURRING HERE: Assigning to 'NSMutableArray *_strong' from incompatible type 'void'// 
} 



- (void)viewDidLoad { 

function_builder = [[NSMutableArray alloc] init];//initialize array// 



[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} else { 
    return YES; 
} 
} 

@end 

回答

0

许多错误有..

您已将此方法(无效*)的结果function_builder这是一种NSMutableArray里的。这是没有意义的。

为了操纵的对象,只发送消息给它:

[function_builder removeLastObject]; // this will remove the last object of the array 
[function_builder removeAllObjects]; // guess what ;) 

对于其他的事:

self.equation_field.text = [function_builder componentsJoinedByString:@", "] 

这将创建与由分隔阵列的所有对象的字符串“ ,“=>A, B, C, D

+0

非常感谢你 – user1520507 2012-07-12 17:18:43

0

我认为这是多么的Xcode/objective- c将数组转换为字符串(如果我错了,纠正我),所以如果你想以不同的方式格式化它,你将不得不遍历整个字符串,并删除括号和逗号,这不应该太老实。

我会这样做的方式是通过字符串读取并复制内容,除非它们是(),或者这样,您的间距仍然正确,并获得所需的过滤效果。

+0

我觉得这部分实际上! self.equation_field.text = [function_builder componentsJoinedByString:@“”用于摆脱标点符号。谢谢你。任何处理错误的建议? – user1520507 2012-07-12 16:44:18

+0

哦,非常好,我完全忘记了那个功能的存在。良好的通话,比我的解决方案更优雅。继续并将其添加为自己的答案并接受它。编辑:我似乎也错过了你提到的其他错误...>。< – 2012-07-12 16:47:04