Passing over Object between Classes not working - objective-c

hey guys,
i'm new to objective-c and i'm having trouble with the memory management. i declared 3 classes, Table, Dataset and my Main class. In my mainclass, i created an Dataset Object and now im trying to pass this Object over to a Tableobject, where i want to store it permanently. but it seems to me that the garbage collector kills the reference before i can use it.
heres some code:
Dataset:
//Dataset.h
#interface Dataset : NSObject {
NSMutableArray* daten;
}
#end
//Dataset.m
#import "Dataset.h"
#import "Datensatz.h"
#implementation Dataset
- (id) init
{
self=[super init];
daten=[[NSMutableArray alloc] init];
return self;
}
Table:
//Table.h
#class Dataset;
#interface Table : NSObject {
Dataset* daten;
}
-(id)init:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
#property (retain) Dataset* daten;
#end
//Table.m
#import "Table.h"
#import "Dataset.h"
#impl
ementation Table
#synthesize daten;
-(id)init:(NSTableView*)aTableView
{
self=[super init];
[self setDaten:[Datenmenge alloc]];
return self;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
NSLog(#"anzahl: %d %#",[self.daten anzahl], self.daten);//This is always 0 null
return [daten anzahl];
}
-(void)setDaten:(Dataset *)a
{
NSLog(#"setter: anzahl: %d %#",[a anzahl], a);
[daten release];
daten=[a retain];
NSLog(#"setter: anzahl: %d %#",[daten anzahl], daten);
}
#end
In my mainclass i do the following:
//init method
[self setDaten:[[[Dataset alloc]init]autorelease]];
tabelle=[[Table alloc] init:tableview];
[tabelle setDaten:[self daten]];
Mainclass:
//code.h
//
// MalWiederWasNeuesAppDelegate.h
// MalWiederWasNeues
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#class Datenmenge,Graph,Tabelle;
#interface MalWiederWasNeuesAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSToolbarItem *datenKnopf;
NSToolbarItem *speichernKnopf;
NSSlider *scaleSlider;
NSScroller *moveSlider;
NSTableView* tableview;
Graph* graph;
Tabelle* tabelle;
Datenmenge* daten;
}
-(void)tuWas;
- (IBAction)datenHinzufuegen:(id)sender;
- (IBAction)speichern:(id)sender;
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSToolbarItem *datenKnopf;
#property (assign) IBOutlet NSToolbarItem *speichernKnopf;
#property (assign) IBOutlet NSSlider *scaleSlider;
#property (assign) IBOutlet NSScroller *moveSlider;
#property (assign) IBOutlet Graph *graph;
#property (assign) IBOutlet Tabelle *tabelle;
#property (assign) IBOutlet NSTableView* tableview;
#property (retain) Datenmenge* daten;
#end
//code.m
//
// MalWiederWasNeuesAppDelegate.m
// MalWiederWasNeues
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MalWiederWasNeuesAppDelegate.h"
#import "Datenmenge.h"
#import "Graph.h"
#import "Tabelle.h"
#implementation MalWiederWasNeuesAppDelegate
#synthesize window;
#synthesize daten;
-(id) init
{
self.daten=[[Datenmenge alloc]init];
[self.daten datenHinzufuegen:nil];
tabelle=[[Tabelle alloc] init:tableview];
tabelle.daten=daten;
NSLog(#"konstruktor: %f %d",[daten maximum],[daten anzahl]);
//graph.daten=daten;
return self;
}
-(void)tuWas{
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (IBAction)datenHinzufuegen:(id)sender
{
NSLog(#"%f %d",[daten maximum],[daten anzahl]);
NSLog(#"daten hinzufügen");
}
- (IBAction) speichern:(id)sender
{
NSLog(#"%# %#",daten,[tabelle daten]);
NSLog(#"speichern");
}
#end
I hope this wasnt too much code for you.
when i call a method of "tabelle", my Table object, "daten" does not refer to an Dataset Object. But the NSLogs in "setDaten" show me valid references.
so, what am i doing wrong?
have a good evening,
lukas

You define Daten as a retain type
#property (retain) Dataset* daten; and #synthesize daten;
theres no need to then implement the method
-(void)setDaten:(Dataset *)a thats what #synthesize daten; does
I think theres a lost in translation moment here so ill assume Table == Tabelle and Dataset == Datmenge and I dont see the implementation for your main class.
cast your eye over this too.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html

Maybe, the daten property of your main class is declared as assign? In that case daten is correct when you call setDaten:, but might have been already autoreleased when you try to access it afterwards.
Also,
-(void)setDaten:(Dataset *)a
{
NSLog(#"setter: anzahl: %d %#",[a anzahl], a);
[daten release];
daten=[a retain];
NSLog(#"setter: anzahl: %d %#",[daten anzahl], daten);
}
is not a good implementation of a setter. If a == daten, then this object will be released (and maybe dealloc'd). You need to check identity of objects when implementing your own setter.

Related

Set text in the textfield does not work in Obj-C

I would like to set an text in the text field in another class and get it from another class. This is something what I want, but it does not work. Can you please help me. Thank you!
aaa.h
#import <Cocoa/Cocoa.h>
#interface aaa : NSImageView {
IBOutlet NSTextField *message;
}
#property (nonatomic, retain) IBOutlet NSTextField *message;
#end
aaa.m
#import "aaa.h"
#import "bbb.h"
#implementation aaa
#synthesize message;
- (void)awakeFromNib {
// [message setStringValue:#"ok, this works!"]; //but i don't want it from here
[self hello];
}
#end
bbb.h
#import <Foundation/Foundation.h>
#interface NSObject (bbb)
- (void)hello;
#end
bbb.m
#import "bbb.h"
#import "aaa.h"
#implementation NSObject (bbb)
- (void)hello {
aaa *obj = [[[aaa alloc] init] autorelease];
[obj.message setStringValue:#"This doesn't work :("]; // set text here, dont work.
NSLog(#"TEST: %#", [obj.message stringValue]);
}
#end
You are using category, so first thing it is used for extending the functionality of existing class. So you cannot set textfield value inside category. But else you can add some functonality after extracting the value. So you have to pass the value inside the category first. Try like this below:
- (void)awakeFromNib {
NSString *resultString=[self hello:#"This doesn't work :("];
[message setStringValue:resultString];
}
#end
#interface NSObject (bbb)
- (NSString*)hello:(NSString*)yourString;
#end
#implementation NSObject (bbb)
- (NSString*)hello:(NSString*)yourString {
return yourString;
}
#end

Why doesn't my #property have an automatic getter?

I've declared a property called Squad, but when I send [self getSquad] I get "no visible #interface for SquadViewController declares the selector 'getSquad'".
SquadViewController.h:
#import "FlipsideViewController.h"
#import "Squad.h"
#interface SquadViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate>
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#property (nonatomic, retain) IBOutlet UILabel *squadNameLabel;
#property Squad *squad;
- (IBAction)updateTitleWithName:(id)sender;
#end
SquadViewController.m:
#import "SquadViewController.h"
#interface SquadViewController ()
#end
#implementation SquadViewController
#synthesize squadNameLabel;
#synthesize squad;
...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![self getSquad]) //<--THIS IS WHERE THE ERROR IS
{
[self setSquad:[Squad squadWithName:#"New Squad"]]; //<-- NOT HERE, SO THE SETTER SEEMS TO EXIST
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
...
I thought that "#synthesize squad" would generate getSquad and setSquad, so I'm confused.
Here's the code for Squad, if for some reason that I don't get it's relevant (I'm new to Objective C, I still find it very confusing (I'm from a java background)):
Squad.h:
#import "SquadBuilderObject.h"
#interface Squad : NSObject
#property NSString *name;
+ (id) squadWithName:(NSString*)name;
#end
Squad.m:
#import "Squad.h"
#implementation Squad
#synthesize name;
+ (id)squadWithName:(NSString *)name
{
Squad *newSquad = [[Squad alloc] init];
[newSquad setName:name];
return newSquad;
}
#end
The standard getter for a property named squad is squad, not getSquad.
The "get…" nomenclature is typically reserved for things returned by reference (e.g. - (BOOL)getSquad:(Squad **)outSquad).

Cocoa : Objects allocated but not properly accessible?

That's what I have :
Class A :
#import "ppCore.h"
#interface ppApplication : NSApplication {
ppCore* core;
}
#property (assign) ppCore* core;
#end
#implementation ppApplication
#synthesize core;
- (void)awakeFromNib
{
[self setCore:[[[ppCore alloc] init] retain]];
}
Class B :
#import "someObject.h"
#import "anotherObject.h"
#interface ppCore : NSObject<NSApplicationDelegate> {
ppSomeObject* someObject;
ppAnotherObject* anotherObject;
}
#property (assign) ppSomeObject* someObject;
#property (assign) ppAnotherObject* anotherObject;
#end
#implementation ppCore
#synthesize someObject, anotherObject;
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
[self setSomeObject:[[ppSomeObject alloc] init]];
[self setAnotherObject:[[ppAnotherObject alloc] init]];
}
And here's the issue :
AT SOME LATER STAGE, in ppApplication, I'm trying to have access to core.
core is there.
But, when I'm trying to access any of core's elements (e.g. [core someObject]), everything is turning up NULL (I've checked it in the Debugger)...
What am I doing wrong??
Have you tried declaring your objects like this:
#property (nonatomic, retain) ppCore* core;
#property (nonatomic, retain) ppSomeObject* someObject;
#property (nonatomic, retain) ppAnotherObject* anotherObject;
I suggest you remove the whole core thing since you can access your delegate through [[NSApplication sharedApplication] delegate] and move the setting of someObject and anotherObject to the delegate's init method.
Antonio is right, bit you need to manage memory as well,
#import "ppCore.h"
#interface ppApplication : NSApplication {
ppCore* core;
}
#property (nonatomic, retain) ppCore* core;
#end
#implementation ppApplication
#synthesize core;
- (void)awakeFromNib
{
ppCore* tempCore = [[ppCore alloc] init];
[self setCore: tempCore];
[tempCore release];
}
This might help.
Why do you believe - (void)applicationDidFinishLaunching: on your ppCore object is ever getting called? It seems to me you'll have to explicitly invoke it from somewhere.

EXC_BAD_ACCESS when synthesizing a 'global' object

this is a follow-up question to my last one here: iOS: Initialise object at start of application for all controllers to use .
I have set my application up as follows (ignore the DB Prefix):
DBFactoryClass // Built a DataManaging Object for later use in the app
DBDataModel // Is created by the factory, holds all data & access methods
DBViewControllerA // Will show some of the data that DBDataModel holds
moreViewControllers that will need access to the same DBDataModel Object
i will go step by step through the application, and will then in the end post the error message i get when building.
AppDelegate.h
#import "DBFactoryClass.h"
AppDelegate.m
- (BOOL)...didFinishLaunching...
{
DBFactoryClass *FACTORY = [[DBFactoryClass alloc ]init ];
return YES;
}
DBFactoryClass.h
#import <Foundation/Foundation.h>
#import "DBDataModel.h"
#interface DBFactoryClass : NSObject
#property (strong) DBDataModel *DATAMODEL;
#end
DBFactoryClass.m
#import "DBFactoryClass.h"
#implementation DBFactoryClass
#synthesize DATAMODEL;
-(id)init{
self = [super init];
[self setDATAMODEL:[[DBDataModel alloc]init ]];
return self;
}
#end
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "DBDataModel.h"
#class DBDataModel;
#interface todayViewController : UIViewController
#property (strong)DBDataModel *DATAMODEL;
#property (weak, nonatomic) IBOutlet UILabel *testLabel;
#end
ViewControllerA.m
#import "todayViewController.h"
#implementation todayViewController
#synthesize testLabel;
#synthesize DATAMODEL;
- (void)viewDidLoad
{
todaySpentLabel.text = [[DATAMODEL test]stringValue];
}
#end
DBDataModel.h
#import <Foundation/Foundation.h>
#interface DBDataModel : NSObject
#property (nonatomic, retain) NSNumber* test;
#end
DBDataModel.m
#import "DBDataModel.h"
#implementation DBDataModel
#synthesize test;
-(id)init{
test = [[NSNumber alloc]initWithInt:4];
return self;
}
#end
when i build it, i get the following error: EXC_BAD_ACCESS in this line:
#synthesize DATAMODEL;
of DBFactoryClass.m
What #synthesize does is to automatically generate implementations of the accessors for a property. EXC_BAD_ACCESS there means that you're accessing garbage when one of the accessors is executed.
That's probably happening here:
[self setDATAMODEL:[[DBDataModel alloc]init ]];
Make sure that DBDataModel's implementation of init actually returns a legitimate object.
As far as I can tell, your DBFactoryClass class is never stored anywhere, and therefore released right after the allocation if you use ARC (Since you use the strong keyword I assumed you do).
- (BOOL)...didFinishLaunching... {
DBFactoryClass *FACTORY = [[DBFactoryClass alloc ]init ];
// If you use ARC this might be released right afterwards
return YES;
}
If you want the factory to be a singleton, use something like this
+ (id)sharedInstance {
static dispatch_once_t once;
static MyFoo *instance;
dispatch_once(&once, ^{
instance = [[self alloc] init];
});
return instance;
}

setting new properties in category interface/implementation

Ok, so I have this, but it wont work:
#interface UILabel (touches)
#property (nonatomic) BOOL isMethodStep;
#end
#implementation UILabel (touches)
-(BOOL)isMethodStep {
return self.isMethodStep;
}
-(void)setIsMethodStep:(BOOL)boolean {
self.isMethodStep = boolean;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.isMethodStep){
// set all labels to normal font:
UIFont *toSet = (self.font == [UIFont fontWithName:#"Helvetica" size:16]) ? [UIFont fontWithName:#"Helvetica-Bold" size:16] : [UIFont fontWithName:#"Helvetica" size:16];
id superView = self.superview;
for(id theView in [(UIView *)superView subviews])
if([theView isKindOfClass:[UILabel class]])
[(UILabel *)theView setFont:[UIFont fontWithName:#"Helvetica" size:16]];
self.font = toSet;
}
}
#end
If I take out the getter and setter methods then it doesn't work it tells me I need to create some getter and setter methods (or use #synthesize - but putting #synthesize in the #implementation throws an error too). But with the getter and setter methods I get an EXC_BAD_ACCESS and a crash. Any ideas? Thanks
Tom
It is not possible to add members and properties to an existing class via a category — only methods.
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html
One possible workaround is to write "setter/getter-like" methods, that uses a singleton to save the variables, that would had been the member.
-(void)setMember:(MyObject *)someObject
{
NSMutableDictionary *dict = [MySingleton sharedRegistry];
[dict setObject:someObject forKey:self];
}
-(MyObject *)member
{
NSMutableDictionary *dict = [MySingleton sharedRegistry];
return [dict objectforKey:self];
}
or — of course — write a custom class, that inherits from UILabel
Note that nowadays an associated object can be injected during runtime. The Objective C Programming Language: Associative References
Checked all answers and did not find the most common solution:
#import <objc/runtime.h>
static void const *key;
#interface ClassName (CategoryName)
#property (nonatomic) BOOL myProperty;
#end
#implementation ClassName (CategoryName)
- (BOOL)myProperty {
return [objc_getAssociatedObject(self, key) boolValue];
}
- (void)setMyProperty:(BOOL)value {
objc_setAssociatedObject(self, key, #(value), OBJC_ASSOCIATION_RETAIN);
}
#end
swift:
private struct AssociatedKeys {
static var keyName = "keyName"
}
extension Foo {
var bar: Any! {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.keyName)
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.keyName , newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
There is actually a way, which may not be ideal, but does work.
For it to work, you will need to create a category for a class X and can only be used on subclasses of the same X (e.g. category UIView (Background) can be used with class MyView : UIView, but not directly with UIView)
// UIView+Background.h
#interface UIView (Background)
#property (strong, nonatomic) NSString *hexColor;
- (void)someMethodThatUsesHexColor;
#end
// UIView+Background.h
#implementation UIView (Background)
#dynamic hexColor; // Must be declared as dynamic
- (void)someMethodThatUsesHexColor {
NSLog(#"Color %#", self.hexColor);
}
#end
Then
// MyView.m
#import "UIView+Background.h"
#interface MyView : UIView
#property (strong, nonatomic) NSString *hexColor;
#end
#implementation MyView ()
- (void)viewDidLoad {
[super viewDidLoad];
[self setHexColor:#"#BABACA"];
[self someMethodThatUsesHexColor];
}
#end
Using this method, you will need to "redeclare" your properties, but after that, you can do all of its manipulation inside your category.
You could inject an associated object during runtime.
#import <objc/runtime.h>
#interface UIView (Private)
#property (nonatomic, assign) CGPoint initialTouchPoint;
#property (nonatomic, strong) UIWindow *alertWindow;
#end
#implementation UIView (Private)
#dynamic initialTouchPoint, alertWindow;
- (CGPoint)initialTouchPoint {
return CGPointFromString(objc_getAssociatedObject(self, #selector(initialTouchPoint)));
}
- (void)setInitialTouchPoint:(CGPoint)initialTouchPoint {
objc_setAssociatedObject(self, #selector(initialTouchPoint), NSStringFromCGPoint(initialTouchPoint), OBJC_ASSOCIATION_RETAIN);
}
- (void)setAlertWindow:(UIWindow *)alertWindow {
objc_setAssociatedObject(self, #selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIWindow *)alertWindow {
return objc_getAssociatedObject(self, #selector(alertWindow));
}
#end
EDIT: Warning: This property would have a unique value for all the instances of the class.
This worked for me, but only because I had only one instance of this class in my app.
#import <AVFoundation/AVFoundation.h>
#interface AVAudioPlayer (AstroAVAudioPlayer)
#property (nonatomic) BOOL redPilot;
#end
#import "AVAudioPlayer+AstroAVAudioPlayer.h"
#implementation AVAudioPlayer (AstroAVAudioPlayer)
BOOL _redPilot;
-(void) setRedPilot:(BOOL)redPilot
{
_redPilot = redPilot;
}
-(BOOL) redPilot
{
return _redPilot;
}
#end
A solution that I found to this was to just give each object that you want flagged a unique tag.
I made a UILabel category to add custom fonts to all my labels but on some i wanted them to be bold so i did this ->
- (void) layoutSubviews {
[super layoutSubviews];
[self addCustomFont];
}
- (void) addCustomFont {
if (self.tag == 22) {
[self setFont:[UIFont fontWithName:SEGOE_BOLD size:self.font.pointSize]];
}else{
[self setFont:[UIFont fontWithName:SEGOE_LIGHT size:self.font.pointSize]];
}
}
It seems as if since Xcode 7 (7.0.1, 7A1001), properties are supported in categories. I noticed that Xcode generates categories now for Core Data subclasses.
For example, I got the files:
Location+CoreDataProperties.h
#import "Location.h"
NS_ASSUME_NONNULL_BEGIN
#interface Location (CoreDataProperties)
#property (nullable, nonatomic, retain) NSNumber *altitude;
#property (nullable, nonatomic, retain) NSNumber *latitude;
#property (nullable, nonatomic, retain) NSNumber *longitude;
#end
NS_ASSUME_NONNULL_END
Location+CoreDataProperties.m
#import "Location+CoreDataProperties.h"
#implementation Location (CoreDataProperties)
#dynamic altitude;
#dynamic latitude;
#dynamic longitude;
#end
So looks like properties in categories might work now. I haven't tested on non-Core Data classes.
What I've noticed is that they do include the category file back into the original class:
Location.h
#interface Location : NSManagedObject
#end
#import "Location+CoreDataProperties.h"
This allows the original class to edit the properties specified by the category.