简体   繁体   中英

Array management help in XCode?

I am creating a program in the iOS SDK in which there is a group of buttons. When a button is clicked, its title is added to an array, and the array is displayed in an assigned label.

When I try to create delete and clear buttons, error messages show up. They show up in the function_builder = function_builder.removeLastObject; and function_builder = function_builder.removeAllObjects; lines of the .m file. The error messages are the same: Assigning to 'NSMutableArray *_strong' from incompatible type 'void'. How do I fix this? Thank you for any and all help

Here is the .h file:

#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

And here is the .m file:

#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

I think that's just how xCode/objective-c converts arrays to strings(Correct me if I'm wrong), so if you want to format it differently you're going to have to iterate through the string and remove the parentheses and commas, which shouldn't be too hard honestly.

The way I'd do it is read through the string and copy the contents unless they are ( ) or , that way your spacing is still correct and you get the filtering effect that you want.

Many errors there..

You've assigned the results of this methods (void*) to function_builder which is of kind NSMutableArray. That makes no sense.

In order to manipulate an object, just send a message to it:

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

For the other thing:

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

This will create a string with all objects in array separated by ", " => A, B, C, D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM