0

我正在 iOS SDK 中创建一个程序,其中有一组按钮。单击按钮时,其标题将添加到数组中,并且数组显示在分配的标签中。

当我尝试创建删除和清除按钮时,会出现错误消息。它们出现在 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
4

2 回答 2

0

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

我这样做的方式是读取字符串并复制内容,除非它们是 ( ) 或 ,这样你的间距仍然是正确的,你会得到你想要的过滤效果。

于 2012-07-12T16:42:06.343 回答
0

那里有很多错误..

您已将此方法 (void*) 的结果分配给类型为 NSMutableArray 的 function_builder。这是没有意义的。

为了操作一个对象,只需向它发送一条消息:

[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

于 2012-07-12T16:47:12.797 回答