Having problems adding objects to array - xcode

In the code below, I am trying to add objects to array. No error, but is not adding objects either. Sorry for asking this pretty basic question. Need help
The NS Object Definition
//DataDefinition.h
#import
#interface DataDefinition : NSObject
#property (nonatomic, retain) NSString *dataHeader;
#property (nonatomic, retain) NSMutableArray *dataDetails;
#end
The DataDefinition Implementation
#import "DataDefinition.h"
#implementation DataDefinition
#synthesize dataHeader;
#synthesize dataDetails;
#end
The Display header section
//DataDisplay.h
#import
#import "DataDefinition.h"
#interface DataDisplay : UITableViewController
#property (strong, nonatomic) NSMutableArray *dataSet;
#property (strong, atomic) DataDefinition *individualData;
#end
The Display implementation section
//DataDisplay.m
#import "DataDisplay.h"
#interface DataDisplay ()
#end
#implementation DataDisplay
#synthesize dataSet;
#synthesize individualData;
- (void)viewDidLoad
{
[super viewDidLoad];
individualData.dataHeader = #"Header1";
individualData.dataDetails = [[NSMutableArray alloc] initWithObjects:#"Header1-Detail1", #"Header1-Detail2", #"Header1-Detail3", nil];
//This didnot add
[dataSet addObject:individualData];
NSLog(#"Count of objects is %d:",[dataSet count]);
//Nor did this
dataSet = [[NSMutableArray alloc] initWithObjects:individualData, nil];
NSLog(#"Count of objects is %d:",[dataSet count]);
self.title = #"DataDisplay";
}

The issue is that individualData is never actually set to an instantiated object (in other words, it is never initialized).
These kinds of oversights are common due to Objective-C's non-error policy regarding sending messages to nil; it's perfectly legal and often useful principle. This means that your code will never complain until you try to pass it to some method which will crash if it sees nil. Unfortunately, you are using initWithObjects, which simply sees nil as the end of the (empty) list. If you had instead tried to use [NSArray arrayWithObject:individualData] you may have seen an error which would hint to you that you had nil instead of an object.
Note that setting properties on nil is particularly tricky, since it looks like you are simply dealing with a C-syle lvalue, when actually it translates to a message-send call at runtime:
individualData.dataHeader = #"Header1";
// is *literally* the same as:
[individualData setDataHeader:#"Header1"];
You can take your pick of solutions. The "cheap" way is to simply initialize it right there. The "better" way (usually) is lazy-instantiation (i.e. in the getter). Since the object is marked as atomic, you likely need to let the compiler write the getter for you, and just initialize it in viewDidLoad (or awakeFromNib, initWithCoder, or similar):
- (void)viewDidLoad
{
[super viewDidLoad];
self.individualData = [[DataDefinition alloc] init];
...

Related

how to store object to NSmutablearray in app delegate?

I'm having a problem with storing and accessing objects with NSmutable array in app delegate. I have tried methods form other websites and stack overlay pages but yet no solution. I want to able to access the array data in another view. Currently nothing is working for me.
Heres my code.
AppDelegate.h :
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSMutableArray* sharedArray;
}
#property (nonatomic, retain) NSMutableArray* sharedArray;
ViewController.h :
#import "AppDelegate.h"
-(void)viewDidLoad{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *model = appDelegate.sharedArray;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"hello" forKey:#"title"];
[dict setObject:#"urlhere" forKey:#"thumbnail"];
[model addObject:dict];
NSLog(#"submitted to array: %#",model);
}
Are you, at any point, initializing the sharedArray? The array must be instantiated before you can add objects to it. For example:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.sharedArray = [NSMutableArray array];
return YES;
}
Having done that, now attempts to add objects to this array from your view controllers should succeed.
Unrelated, but you should not define instance variables for your properties. Let the compiler synthesize that for you, e.g.:
AppDelegate.h:
#interface AppDelegate : UIResponder <UIApplicationDelegate>
// {
// NSMutableArray* sharedArray;
// }
#property (nonatomic, retain) NSMutableArray* sharedArray;
#end
What you have is technically acceptable, but it's inadvisable because of possible confusion between this sharedArray instance variable and the what the compiler will synthesize for you (e.g. if you don't have a #synthesize line, the compiler will automatically create an instance variable called _sharedArray, with a leading underscore, for you). Even if you had a #synthesize line that ensured that the instance variable was correct, having the explicitly declared instance variable is simply redundant.

Trouble saving segmented control with core data

I thought this issue would be a simple one to over come, but it's providing to be a little harder and frustrating.
I'm using UISegmentedControl to make a selection and then saved to core data with a NSNumber entity. Unfortunately when I try to convert the integer from the segment control into a NSNumber, it comes up (null).
Below is a sample code of my problem.
// ViewController.h
#import <UIKit/UIKit.h>
#import "Object.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISegmentedControl *segmentOutlet;
#property (strong, nonatomic) Object *currentObject; //core data object with an entity named subObject as an NSNumber
- (IBAction)segmentControlSelected:(id)sender;
#end
//ViewController.m
-(IBAction)segmentControllerSelected
{
[self.segmentOutlet selectedSegmentIndex] // segment control
currentObject.subObject = [NSNumber numberWithInt:([self.segmentOutlet selectSegmentIndex]);
NSLog(#"the another Object number is %#", currentObject.subObject);
}
The NSLog %# result is (null), so I'm not sure how the correct this.
I've only started to learn programming, so it's probably a basic rookie mistake.
Thanks for any help
Your segmentOutlet needs to be specified as strong. The memory system is discarding it at some point; then, when you try self.segmentOutlet the return value is nil. After than invoking selectSegmentIndex fails and, by default, returns nil and you eventually get to 'null'. Try:
#property (strong, nonatomic) IBOutlet UISegmentedControl *segmentOutlet;

Trying to use KVC in Xcode to update a TextLabel

I've got a TextField that I'm trying to update the values as it progresses through running my code using KVC. Unfortunately I cannot seem to get anything to update past the initial value.
I've used the bindings on the button that launches the code, the TextField that I want to update and it just doesn't want to update. Please forgive me for the n00bish question but I've been searching online all day, going through tutorials, rewriting the code different ways and can't seem to figure out why this very simple tasks won't work.
Here is my KVC.h file:
#import <Foundation/Foundation.h>
#interface KVC : NSObject{
NSString *_progressString;
}
#property (nonatomic, retain) NSString *progressString;
#end
Here is my App header file:
#import <Cocoa/Cocoa.h>
#import "KVC.h"
//UI Controls
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
NSPersistentStoreCoordinator *__persistentStoreCoordinator;
NSManagedObjectModel *__managedObjectModel;
NSManagedObjectContext *__managedObjectContext;
NSButton *_loadingExtracts;
NSButton *_processStuff;
NSProgressIndicator *_progressBar;
KVC *myProgressString;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (assign) IBOutlet NSButton *loadingExtracts;
#property (assign) IBOutlet NSButton *processStuff;
#property (assign) IBOutlet NSProgressIndicator *progressBar;
- (IBAction)saveAction:(id)sender;
- (IBAction)loadingExtracts:(id)sender;
- (IBAction)processStuff:(id)sender;
#end
And finally, here is the function inside the code that I cannot get to update.
- (IBAction)processStuff:(id)sender
{
KVC *frickenHeck = [[KVC alloc] init];
NSLog(#"Button Pressed - Processing Information");
[myProgressString setValue:#"Testing" forKey:#"_progressString"];
[_progressBar setUsesThreadedAnimation:YES];
[_progressBar startAnimation:self];
//Turn off Progress Bar
[_progressBar stopAnimation:self];
[frickenHeck setValue:#"Completed" forKey:#"_progressString"];
//[_progressText setStringValue:#""];
}
(As you can see, I've tried updating 2 different ways and neither work. The allocation seems to set up the initial variable just fine, the Log shows I'm in the method, just can't get my label to update past the allocation).
Any thoughts or ideas would be greatly appreciated.
I am more of a iOS developer, so I am not 100% certain how some of the standards cross-over to the OSX side of things. Nevertheless,
You are saying that you are using KVC, like it is a framework providing a easy to use key-value coding scheme, which doesn't necessarily make sence. This appears to be a custom class you created called KVC.
In your KVC.m file did you #synthesize this variable?
#implementation KVC
#synthesize progressString = _progressString;
You have defined your object frickenHeck as class KVC which holds a property called progressString, that you are making available to other classes via this call.
#property (nonatomic, retain) NSString *progressString;
Assuming you have synthesized the variable in your #implementation file, why don't you just call:
frickenHeck.progressString = #"Testing";
or
frickenHeck.progressString = #"Completed";
Sure you can set the variable via Key Value Coding, but not by setting to the private form of your class variable. Try:
[frickenHeck setValue:#"Completed" forKey:#"progressString"];
So, have you synthesized this variable? Or at least set a accessor setter/getter for the variable? You typically only want to do one or the other.
Header:
- (void)progressString;
- (NSString *)setProgressString:(NSString *)_string;
Implementation:
- (void)progressString {
return _progressString;
}
- (NSString *)setProgressString:(NSString *)_string {
_progressString=_string;
}
Also to note that if you are going to be changing this variable a lot, you may want to use the NSMutableString form of the class, and set the #property declaration to copy. #property (nonatomic, copy) NSMutableString *progressString;
I hope some of this information assists you on your journey..
Mark

NSString out of scope

I used the codes below to set a NSString
#import <Foundation/Foundation.h>
#interface AppController : NSObject
{
NSString *myString;
}
#property (nonatomic, retain) NSString *myString;
#end
#import "AppController.h"
#implementation AppController
#synthesize myString;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *zs0= [[NSString alloc] initWithFormat: #"abc"];
myString =[zs0 retain];
[zs0 release];//breakpoint
}
- (void)dealloc {
[myString release];
[super dealloc];
}
#end
when I check the value of myString at the breakpoint
it always said 'out of scope'
Welcome any comment
You don't need the temp variable zs0 - just assign it to the property
You don't need initWithFormat since you're not formatting.
You don't need to alloc the string - if you call [NSString stringWithString] it will autorelease it and then when you assign it to a (retain) property, it will retain it.
You're over retaining. You alloc the string which gives it a retain count of 1, then you retain it which gives it 2, then you assign it to a retained property which retains it again.
One simple way is:
self.myString = [NSString stringWithString:#"abc"];
That will create a string that's autoreleased (not created with alloc, copy by convention) and then the property will retain it.
Why are you using initWithFormat if you aren't using a format?Next, why even allocate zs0 if you aren't going to use it? Just set myString to what you want it to be.
Thus, your code should look like this:
myString = [[NSString alloc] initWithString: #"abc"];
Try that and everything should work.

Cocoa class not displaying data in NSWindow

I have one class that controls one window, and another class that controls a different window in the same xib, however, the second window never displays what it should.
In the first class I alloc and init the second class, then pass some information to it. In the second class it displays that data in the table view.
Yes, in the .xib I have all the connections set up correctly, I've quadruple checked. Also the code is correct, same with the connections, I've quadruple checked.
Edit: and yes, there's data in the arrays, and the classes are NSObjects.
Edit2: I kinda found the problem. For some reason, the array is filled with contents, but it's returning 0 as a count.
Edit 9000:
Here's the code:
Answer.h
#import <Cocoa/Cocoa.h>
#interface MSAnswerView : NSObject {
IBOutlet NSWindow *window;
NSArray *User;
NSArray *Vote;
NSArray *Text;
IBOutlet NSTableView *view;
IBOutlet NSTableColumn *voteCount;
IBOutlet NSTableColumn *saidUser;
IBOutlet NSTextView *body;
}
-(void)setUpWithVoteCount:(NSArray *)array User:(NSArray *)user Text:(NSArray *)text;
#property (nonatomic, retain) NSWindow *window;
#property (nonatomic, retain) NSTableView *view;
#property (nonatomic, retain) NSTableColumn *voteCount;
#property (nonatomic, retain) NSTableColumn *saidUser;
#property (nonatomic, retain) NSTextView *body;
#end
.m
#import "MSAnswerView.h"
#implementation MSAnswerView
#synthesize view;
#synthesize voteCount;
#synthesize saidUser;
#synthesize body;
#synthesize window;
-(void)awakeFromNib
{
[view setTarget:self];
[view setDoubleAction:#selector(bodydata)];
[view reloadData];
}
-(void)setUpWithVoteCount:(NSArray *)array User:(NSArray *)user Text:(NSArray *)text
{
Vote = array;
User = user;
Text = text;
if (window.isVisible = YES) {
[view reloadData];
[view setNeedsDisplay];
}
}
-(int)numberOfRowsInTableView:(NSTableView *)aTable
{
return [User count];;
}
-(id)tableView:(NSTableView *)aTable objectValueForTableColumn:(NSTableColumn *)aCol row:(int)aRow
{
if (aCol == voteCount)
{
return [Vote objectAtIndex:aRow];
}
else if (aCol == saidUser)
{
return [User objectAtIndex:aRow];
}
else
{
return nil;
}
}
-(void)bodydata
{
int index = [view selectedRow];
[body setString:[Text objectAtIndex:index]];
}
#end
The problems in your code are numerous.
For one thing, this comparison in -setUpWithVoteCount:User:Text: is incorrect:
window.isVisible = YES
That should be the comparison operator, == not the assignment operator =.
Secondly, you are naming your ivars and methods incorrectly. Instance variables (in fact, variables of any type) should start with a lower-case letter. This is to distinguish them from class names. Check out the Apple coding guidelines.
I'd also suggest that a name like text is a bad name for a variable that stores a collection like an NSArray. Instead, you should name it something like textItems so it's clear that the variable represents a collection and not a single string.
Also, the class itself is poorly named. You have called it MSAnswerView but it's not a view, it's some type of window controller. At the very least call it MSAnswerWindowController. Better still would be to make it a subclass of NSWindowController and make it File's Owner in its own nib. This is the standard pattern for window controllers.
Your method -setUpWithVoteCount:User:Text: should really be an initializer:
- initWithVoteCount:user:text:
That way it's clear what it's for and that it should be called once at object creation time.
The main problem, however, is that you're not retaining the values that you pass in to your setup method. That means that if no other object retains a reference to them, they will go away at some indeterminate point in the future. If you access them at a later time, you will crash or at the very least receive bad data, which is what's occurring.
Of course, you must also add a -dealloc method in this case to ensure you release the objects when you're finished with them.
Putting all those suggestions together, your class should really look something like this:
MSAnswerWindowController.h:
#import <Cocoa/Cocoa.h>
//subclass of NSWindowController
#interface MSAnswerWindowController : NSWindowController <NSTableViewDataSource>
{
//renamed ivars
NSArray *users;
NSArray *voteCounts;
NSArray *textItems;
IBOutlet NSTableView *view;
IBOutlet NSTableColumn *voteCount;
IBOutlet NSTableColumn *saidUser;
IBOutlet NSTextView *body;
}
//this is now an init method
- (id)initWithVoteCounts:(NSArray *)someVoteCounts users:(NSArray *)someUsers textItems:(NSArray *)items;
//accessors for the ivars
#property (nonatomic, copy) NSArray* users;
#property (nonatomic, copy) NSArray* voteCounts;
#property (nonatomic, copy) NSArray* textItems;
#property (nonatomic, retain) NSWindow *window;
#property (nonatomic, retain) NSTableView *view;
#property (nonatomic, retain) NSTableColumn *voteCount;
#property (nonatomic, retain) NSTableColumn *saidUser;
#property (nonatomic, retain) NSTextView *body;
#end
MSAnswerWindowController.m:
#import "MSAnswerWindowController.h"
#implementation MSAnswerWindowController
//implement the init method
- (id)initWithVoteCounts:(NSArray*)someVoteCounts users:(NSArray*)someUsers textItems:(NSArray*)items
{
//this is an NSWindowController, so tell super to load the nib
self = [super initWithWindowNibName:#"MSAnswerWindow"];
if(self)
{
//copy all the arrays that are passed in
//this means we hold a strong reference to them
users = [someUsers copy];
voteCounts = [someVoteCounts copy];
textItems = [items copy];
}
return self;
}
//make sure we deallocate the object when done
- (void)dealloc
{
self.users = nil;
self.voteCounts = nil;
self.textItems = nil;
[super dealloc];
}
//this is called when the window first loads
//we do initial window setup here
- (void)windowDidLoad
{
[view setTarget:self];
[view setDataSource:self];
[view setDoubleAction:#selector(bodydata)];
}
//this is called when the view controller is asked to show its window
//we load the table here
- (IBAction)showWindow:(id)sender
{
[super showWindow:sender];
[view reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView*)aTable
{
return [users count];
}
- (id)tableView:(NSTableView*)aTable objectValueForTableColumn:(NSTableColumn*)aCol row:(NSInteger)aRow
{
if (aCol == voteCount)
{
return [voteCounts objectAtIndex:aRow];
}
else if (aCol == saidUser)
{
return [users objectAtIndex:aRow];
}
return nil;
}
- (void)bodydata
{
NSInteger index = [view selectedRow];
[body setString:[textItems objectAtIndex:index]];
}
#synthesize users;
#synthesize voteCounts;
#synthesize textItems;
#synthesize view;
#synthesize voteCount;
#synthesize saidUser;
#synthesize body;
#end

Resources