NSMutableDIctionary setObject: ForKey crashing when Key is member of Object - objective-c

I'm trying to use an NSMutableDictionary to pair my custom classes object with a UIButton as it's key. The UIButton that is the key is also a member of the object that I want to store as the object in the NSMutableDictionary.
My Class definition looks like ths:
#interface ClassA : NSObject {
#public
UIButton *button1;
UIButton *button2;
UILabel *label;
}
#property (nonatomic, retain) UIButton *button1;
#property (nonatomic, retain) UIButton *button2;
#property (nonatomic, retain) UILabel *label;
#end
And my implementation is just this:
#implementation ClassA
#synthesize button1, button2, label;
#end
The NSMutableDictionary is in another class. I define it in the implementation like this:
#interface UIClass1 : UIViewController {
NSMutableDictionary *Dictionary;
}
-(void)DoWork;
#property (nonatomic, retain) NSMutableDictionary *Dictionary;
#end
And the part where I'm trying to set the Dictionary values is done here:
-(void)DoWork{
Dictionary = [[NSMutableDictionary alloc] init];
ObjectA = [[ClassA alloc] init];
ObjectA->button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ObjectA->button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ObjectA->label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)] autorelease]
/* THIS IS A TEST AND IT WORKS FINE */
NSString *str = [[NSString alloc] initWithFormat:#"test"];
[Dictionary setObject:obj1 forKey:str];
/*THIS IS WHAT I WOULD ACTUALLY LIKE TO DO */
[Dictionary setObject:Object1 forKey:ObjectA->button1];
[Dictionary setObject:ObjectA forKey:ObjectA->button2];
}
I've followed this code through my debugger and when I get to this line:
[Dictionary setObject:Object1 forKey:ObjectA->button1];
It just crashes SIGABRT. None of my variables are nil, every object has been allocated.
Any ideas as to why I can't set the key to the button from ClassA but I can set it the the NSString I created as a test?

The object you use as a key in an NSMutableDictionary must conform to the NSCopying protocol.
From Apple docs:
The key for value. The key is copied (using copyWithZone:; keys must
conform to the NSCopying protocol). The key must not be nil.
UIButton does not conform to NSCopying.

Try using
forKey:[NSValue valueWithPointer:ObjectA->button1]

Related

Unable to add string into an mutable array

I am trying to upload assignments into a table in an different view controller. First when i click on the upload button this code runs.
- (IBAction)uploadAssignment:(id)sender {
nameOfAssignmentAsString = self.nameOfAssignment.text;
NSLog(#"%#", nameOfAssignmentAsString);
NSInteger row;
row = [self.subjectPicker selectedRowInComponent:0];
subjectOfAssignmentAsString = [subjectArray objectAtIndex:row];
NSLog(#"%#", subjectOfAssignmentAsString);
NSDate *deadline = [self.deadlinePicker date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
deadlineOfAssignmentAsString = [dateFormat stringFromDate:deadline];
NSLog(#"%#",deadlineOfAssignmentAsString);
HomeworkViewController *HVC;
[HVC.nameOfAssignmentInAnArray addObject:nameOfAssignmentAsString];
[HVC.SubjectOfAssignmentInAnArray addObject:subjectOfAssignmentAsString];
[HVC.deadlineOfAssignmentInAnArray addObject:deadlineOfAssignmentAsString];
NSLog(#"%#",HVC.nameOfAssignmentInAnArray.count);
}
then in my HomeworkViewController.h this is the code
#interface HomeworkViewController : UIViewController <UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate> {
int i;
}
#property (strong, nonatomic) IBOutlet UIView *mainMenu;
#property (strong, nonatomic) IBOutlet UIButton *homeworkButton;
#property (strong, nonatomic) IBOutlet UIButton *uploadButton;
#property (strong, nonatomic) NSMutableArray *nameOfAssignmentInAnArray;
#property (strong, nonatomic) NSMutableArray *SubjectOfAssignmentInAnArray;
#property (strong, nonatomic) NSMutableArray *deadlineOfAssignmentInAnArray;
I did initialise it in my HomeworkViewController.m file. But for some reason the string is not being added into the array.
You declare HVC but never assign a value to it. Therefore it cannot hold anything in itself. But for some reason the designers of Objective-C decided it's not an error to send messages to nil objects, so this might not be noticed.

NSArrayController does not update content for second window

I'm writing an OSX app where a second window is opened to show results when the button on the first window is pushed. The window-2 start fine and shows what I want. But when I change inputs in window-1 and hit the action button again the window-2 doesn't update the results.
here my questions:
how does the content of window-2 update after input change in window-1
how is window-2 closed and released (right now window-2 shows up with the same content before closed when action button is pushed again)
here is the code for the action button:
- (IBAction)pushRun:(id)sender {
if (!rwc)
{
rwc = [[ResultWindowController alloc] init];
[rwc setValueArray:[toDoItemArrayController arrangedObjects]];
[rwc setNumberOfCalculations:[NSNumber numberWithInt:[_inputNumberOfCalculations intValue]]];
[rwc calculateResults]; //starts method in 2nd-window controller for result calculation
}
[rwc showWindow:self];
}
It might be easy but I'm afraid to always create an other ResultWindowController instance.
Thanks in advance.
Joerg
here is the ResultWindowController.h:
#import <Cocoa/Cocoa.h>
#interface ResultWindowController : NSWindowController{
NSArray *valueArray;
NSMutableArray *resultArray;
NSNumber *numberOfCalculations;
}
#property (nonatomic, retain, readwrite) NSArray *valueArray;
#property (retain) NSNumber *numberOfCalculations;
#property (nonatomic, retain, readwrite) NSMutableArray *resultArray;
-(void)calculateResults;
#end
and here the ResultWindowController.m
#import "ResultWindowController.h"
#import "ResultItem.h" //my result model
#implementation ResultWindowController
#synthesize valueArray, resultArray, numberOfCalculations;
- (id)init
{
if(![super initWithWindowNibName:#"ResultWindow"])
return nil;
return self;
}
-(void)awakeFromNib
{
}
- (void)windowDidLoad
{
[super windowDidLoad];
}
- (void)calculateResults
{
//a lot of calculation code ...
ResultItem *newResult = [[ResultItem alloc]init];
[newResult setValue:[nameArray objectAtIndex:i] forKey:#"name"];
[newResult setValue:[NSNumber numberWithDouble:avg] forKey:#"averageValue"];
[newResult setValue:[NSNumber numberWithDouble:min] forKey:#"minValue"];
[newResult setValue:[NSNumber numberWithDouble:max] forKey:#"maxValue"];
[newResult setValue:dimensionRandomArray forKey:#"randomArray"];
[resultArray addObject:newResult];
}
resultarray is the content source for an arraycontroller in the ResultWindowController.xib. The arraycontroller is bound to a table view which is supposed to show the array content. This is not updated the second time.
I believe you must simply do the following:
- (IBAction)pushRun:(id)sender {
if (!rwc)
{
rwc = [[ResultWindowController alloc] init];
[rwc setValueArray:[toDoItemArrayController arrangedObjects]];
[rwc setNumberOfCalculations:[NSNumber numberWithInt:[_inputNumberOfCalculations intValue]]];
[rwc calculateResults]; //starts method in 2nd-window controller for result calculation
}
[rwc setValueArray:[toDoItemArrayController arrangedObjects]];
[rwc setNumberOfCalculations:[NSNumber numberWithInt:[_inputNumberOfCalculations intValue]]];
[rwc calculateResults]; //starts method in 2nd-window controller for result
[rwc showWindow:self];
}
You were only changing values of ResultWindowController if it did not exist. You want to change values no matter what, just not start a new instance. So, as long as you don't use [[alloc]init] you're good.
Hope that helps. If you need anything else, drop a comment.
Edit
To create a property for your tableView do the following:
In .h
#import <Cocoa/Cocoa.h>
#interface ResultWindowController : NSWindowController{
NSArray *valueArray;
NSMutableArray *resultArray;
NSNumber *numberOfCalculations;
}
#property (nonatomic, retain, readwrite) NSArray *valueArray;
#property (retain) NSNumber *numberOfCalculations;
#property (nonatomic, retain, readwrite) NSMutableArray *resultArray;
#property (assign) NSTableView *yourTableView; //Add this code
-(void)calculateResults;
#end
and here the ResultWindowController.m
#import "ResultWindowController.h"
#import "ResultItem.h" //Your result model
#implementation ResultWindowController
#synthesize valueArray, resultArray, numberOfCalculations;
#synthesize yourTableView; //Add this code
Then you should be able to call [yourTableView reloadData]

iOS Passing (Retain) the value of an NSMutableArray to another NSMutableArray in another view

I'm using .XIB and without ARC. I'm passing the value of the NSMultableArray to another view, if I put [self presentModel...], it works, but if I call the AnotherView with a button the value of the NSMultableArray of the AnotherView is null!
AnotherView.h
#interface AnotherViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *otherAnother;
NSMutableArray *arrayOfTheAnotherView;
}
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#property (retain, nonatomic) NSMutableArray *arrayOfTheAnotherView;
AnotherView.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
otherAnother = [[NSMutableArray alloc]init];
otherAnother = [[NSMutableArray alloc]initWithArray:self.arrayOfTheAnotherView];
//    [otherAnother addObjectsFromArray:arrayOfTheAnotherView];
NSLog(#"%#", self.arrayOfTheAnotherView);
}
The NSLog has written "null"
CurrentView.h
#interface CurrentViewController : UIViewController {
NSMutableArray * arrayCurrentView;
AnotherViewController *superAnotherView;
}
#property (retain, nonatomic) AnotherViewController *superAnotherView;
CurrentView.m
#synthesize superAnotherView;
NSString *x = [[NSString alloc]initWithFormat:#"%#",[label text]];
arrayCurrentView = [[NSMutableArray alloc]init];
[arrayCurrentView retain];
[arrayCurrentView addObject:x];
self.superAnotherView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
[self.superAnotherView.arrayOfTheAnotherView addObjectsFromArray:arrayCurrentView];
I don't know how to retain the value of the NSMultableArray, thanks the help.
It is how I call the AnotherView:
UIButton *buttonAnother = [UIButton buttonWithType:UIButtonTypeCustom]; [buttonAnother setTag:5]; [buttonAnother addTarget:self action:#selector(switchTabBar:) forControlEvents:UIControlEventTouchDown];
[tabBarViewController.view addSubview:buttonAnother];
- (IBAction)switchTabBar:(id)sender { switch ([(UIButton *)sender tag]) { case 5: [self.tabBarController setSelectedIndex:0]; break; }
These should not be necessary:
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
Properties are already initialized when the class is initialized.
In fact, your explicit retain lines shouldn't be necessary at all; you're not releasing them as far as I can see and just making the retain count 2 so that it has to be released twice.
I think there's something going on here that the methods and view controllers are not being called in the order you want, probably something to do with the tab bar.
I dicoverd! I'm using the MVC
A array of the APP delegate don't lost its value.
AppDelegate
NSMutableArray *arrayDelegate;
View.m
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate.arrayDelegatePedido addObject:#"title"]; //for example

Gaining access to an NSMutableArray

I am new to arrays and objects. I have a class HowToPlay.h (of course that has a .m also) their i define two arrays
NSMutableArray *nibs;
NSMutableArray *unusedNibs;
#property (nonatomic, retain) NSMutableArray *nibs;
#property (nonatomic, retain) NSMutableArray *unusedNibs;
then we jump into the .m,
i write all the stuff for the arrays,
nibs = [[NSMutableArray alloc]initWithObjects:#"Question 2", #"Question 3", nil];
self.unusedNibs = nibs;
[nibs release];
Then i also have another class called Question 1, i need to be able to use this array in that class, and be able to change it, but keep the changes on the HowToPlay.m file.
here is why, basically this array loads random NIB files, and then deletes them from the array when they have been used.
in Question 1.m here is what im doing to use the array
random = arc4random() % [self.unusedNibs count];
NSString *nibName = [self.unusedNibs objectAtIndex:random];
[self.unusedNibs removeObjectAtIndex:random];
if (nibName == #"Question 3") {
Question_3 *Q3 = [[Question_3 alloc] initWithNibName:#"Question 3" bundle:nil];
Q3.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q3 animated:YES];
[Q3 release];
}
if (nibName == #"Question 2") {
Question_2 *Q2 = [[Question_2 alloc] initWithNibName:#"Question 2" bundle:nil];
Q2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q2 animated:YES];
[Q2 release];
}
This all seems to work fine, but the problem im having is it seems that objects in the array are not getting deleted, even though this line runs
[self.unusedNibs removeObjectAtIndex:random];
I haved tried making it
[HowToPlay.unusedNibs removeObjectAtIndex:random];
I get a error saying expected '.' before '.' token
It seems to me that i have access to read the array, but not to change it. any way to fix this so i can change the array? thanks
UPDATE:
here is the whole HowToPlay.h file contents:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
int score;
int usedQ1;
int usedQ2;
int usedQ3;
NSMutableArray *nibs;
NSMutableArray *unusedNibs;
#interface HowToPlay : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *selectType;
NSMutableArray *selectArray;
AVAudioPlayer *audioPlayer;
UIActivityIndicatorView *progress;
UIButton *worldButton;
UIButton *politicsButton;
UIButton *starButton;
}
#property (nonatomic, retain) NSMutableArray *nibs;
#property (nonatomic, retain) NSMutableArray *unusedNibs;
#property (nonatomic, retain) IBOutlet UIButton *worldButton;
#property (nonatomic, retain) IBOutlet UIButton *politicsButton;
#property (nonatomic, retain) IBOutlet UIButton *starButton;
#property (nonatomic, retain) IBOutlet UIPickerView *selectType;
#property (nonatomic) int usedQ1;
#property (nonatomic) int usedQ2;
#property (nonatomic) int usedQ3;
#property (readwrite) int score;
-(IBAction)World:(id)sender;
- (IBAction)Politics:(id)sender;
-(IBAction)Stars:(id)sender;
#end
#import "MainViewController.h"
#import "Question 1.h"
#import "Question 2.h"
#import "Question 3.h"
I import after because otherwise i get errors
Also I have the array set up on theViewDidLoad part of HowToPlay, is this a bad idea?
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
NSURL *click = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/click.wav", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:click error:nil];
audioPlayer.numberOfLoops = 1;
progress = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
progress.frame = CGRectMake(0.0, 0.0, 30.0, 30.0);
progress.center = self.view.center;
[self.view addSubview: progress];
usedQ1 = 0;
usedQ2 = 0;
usedQ3 = 0;
selectArray = [[NSMutableArray alloc]init];
[selectArray addObject:#"World"];
[selectArray addObject:#"Politics"];
[selectArray addObject:#"Stars"];
score = 0;
nibs = [[NSMutableArray alloc]initWithObjects:#"Question 2", #"Question 3", nil];
self.unusedNibs = nibs;
[nibs release];
}
How do you check if objects are actually deleted? Please post some more code as the way you remove objects from unusedNibs seems to be fine. As per this code:
[self.unusedNibs removeObjectAtIndex:random];
and this code
[HowToPlay.unusedNibs removeObjectAtIndex:random];
The reason why you're getting this error is because you're trying to access unusedNibs using a class name "HowToPlay" instead of its instance "self".
A couple of things: first, to call properties like unusedNibs in HowToPlay it looks like you are calling the class and not an instance. You need to create a HowToPlay object and assign it to a property in your Question1 object so the question one Object has something to call to. Question1 should not call 'self' for unusedNibs since it does not own it.
With what you are doing it might make more sense to put the function figuring out the next random controller in the HowToPlay class. That way your question view controllers could just ask it to return which controller is next without having to duplicate that code across every question controller.

Share NSArray Contents between multiple methods in a single class

What am I doing wrong? My code crashes when I try to log the array. Here is my class:
#interface ArrayTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSArray *array;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) NSArray *array;
-(IBAction)buttonPressed;
#end
#implementation ArrayTestAppDelegate
#synthesize window, array;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
array = [NSArray arrayWithObjects:#"Banana", #"Apple", #"Orange", #"Pear", #"Plum", nil];
[window makeKeyAndVisible];
}
-(IBAction)buttonPressed {
NSLog(#"%#", array);
}
- (void)dealloc {
[window release];
[array release];
[super dealloc];
}
#end
This is a common memory management error in Cocoa. The arrayWithObjects method of the NSArray class returns an autoreleased object. By the time you try to log the array in the buttonPressed method, the array has already been released and you get a crash. The fix is easy:
array = [[NSArray alloc] initWithObjects:#"Banana", #"Plum", nil];
Or:
array = [[NSArray arrayWithObjects:#"Banana", #"Plum", nil] retain];
I guess the first one is better, the retain on the end of the second example is easy to miss. I would suggest that you read some more on memory management in Cocoa.