NSMutableString and setting up accessor methods Objective-C 2.0 - accessor

There is a copy attribute for synthesised accessor methods ex:
#property (copy) NSMutableString *string;
When assign using this setter method it seems to always call the copy method, even though I would like to create a mutable copy of what ever I assign to string, during assignment of the instance variable string.
Is this a know issue and are there any workarounds?
Thanks :-)

Don't call #synthesize string in your .m implementation file and instead write your own getter.
e.g.
- (NSMutableString *) string
{
NSMutableString * stringToReturn = [NSMutableString stringWithString: someStringObject];
}
More information about properties (and what to do when you don't do #synthesize) can be found at:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
p.s. the property name "string" may be confusing to anyone else who looks at your code down the road... I'd recommend changing that to be more program-specific

Related

Global Variables Xcode

Hi I have a relative strange Problem and it costs me a lot of nerves :-)
I have a *.h file with content of
#property (nonatomic, retain) NSString *name;
I synthized it also in the *.m file
#synchronized name;
Works great but I have a (void) Function in my code which writes a string into Variable name like:
name = #"Test";
And in an IBAction of a button I would like to use the stored string of name but name is empty in the IBAction part.
in the IBAction
NSlog(#"%#",name);
What are my problems?
Maybe someone can help.

How can I bind to NSTableColumn's headerTitle?

I would like to bind NSTableColumn's headerTitle property to an NSMutableArray in my model layer (via an NSArrayController).
Basically I want to have an array where I can change values and have the table column header titles update. Is that reasonable?
However, the headerTitle binding wants an single NSString and I'm not sure how to connect my model object to this binding via my NSArrayController. Google does not give many hits for this problem.
My model layer consists of two class (both of which are appropriately KVC compliant). The first is a model which represents a single column title, it has one property title,
// A model class representing the column title of single NSTableColumn
#interface ColumnTitle : NSObject
#property NSString *title;
+ (ColumnTitle*) columnTitleWithTitle:(NSString*) aString;
#end
The second a model object which represents an ordered group of ColumnTitle objects,
// Class representing an order collection of model items
#interface TableColumnTitles : NSObject
#property NSMutableArray* columnTitles; // an array of ColumnTitle objects
// These are the KVC array accessors
-(void) insertObject:(ColumnTitle*)columnTitle inColumnTitlesAtIndex:(NSUInteger)index;
- (void)removeObjectFromColumnTitlesAtIndex:(NSUInteger)index;
- (void)replaceObjectInColumnTitlesAtIndex:(NSUInteger)index withObject:(ColumnTitle*)columnTitle;
#end
Note that TableColumnTitles object implements the above array accessors which are required for the bindings. Any suggestions?
Haven't tried that before but what you're actually asking for is using KVC for array indexes. A quick google didn't turn up anything on that issue except some results that indicate it's not (yet) possible (check this)
The easiest work-around I could come up with would be to simply add dedicated properties for the array indexes.. not nice but does the job.
So for a NSMutableArray called myArray and contains objects with title properties of type NSString you'd do something like:
#property (nonatomic, readonly, getter = columnOneGetter) NSString *columnOneString;
(NSString*) columnOneGetter
{
return myArray[0].title;
}
Always assuming of course their number is known in advance and we're not talking 200 columns :-)
I think this may/may not be what you're after, but quick google search landed me here:
http://pinkstone.co.uk/how-to-add-touch-events-to-a-uitableviewfooter-or-header/
edit: i realize this is for mac (not ios) but should be pretty easy to translate if it actually helps.

NSArrayController reallocating array?

I have an NSCollectionView whose content is bound to an NSArrayController's arrangedObjects. When I call addObject: on the array controller, it seems to reallocate the underlying array - I can observe the pointer changing addresses. This isn't acceptable behavior for my particular case, as other objects also depend on the array.
Is this normal behavior or am I doing something wrong? I've seen some alternative solutions, such as directly modifying the array and calling willChangeValueforKey: and didChangeValueForKey: on the controller, but that doesn't seem like the most elegant solution.
I am fairly new to Objective-C and Cocoa, so go easy on me. :)
Thanks!
#interface NSArrayController : NSObjectController {
#private
void *_reserved4;
id _rearrangementExtensions;
NSMutableArray *_temporaryWorkObjects;
NSUInteger _observedIndexHint;
NSMutableIndexSet *_selectionIndexes;
NSMutableArray *_objects;
NSIndexSet *_cachedSelectedIndexes;
NSArray *_cachedSelectedObjects;
NSArray *_arrangedObjects;
}
If you look at NSArrayController's header, the instance variable for arrangedObject is a NSArray, so it will have to create a new array whenever you add a new object and is a normal behaviour.

global NSMutableArray variable xcode

I am making an iPhone application that goes through a set of viewcontrollers that collect data from user input. At the end of the views I encapsulate all of the data in an customized object called GELObject. Now I need to save this data in a NSMutableArray somewhere so that it can be accessed by a tableviewcontroller that is another branch off of the rootviewcontroller. I was thinking a global variable from the tableviewcontroller, but I did some research and I am reading about singleton's in the appdelegate. Some guidance would be greatly appreciated and if you're feeling extra generous a quick explanation of how to make and use singleton's because it is intriguing me.
Thanks!
To manage a singleton you create a global variable (can be restricted to one file's scope with static) with an initial sentinel value of nil, and use a class method to create the singleton on the first call.
For example:
static Something* globalSomething = nil;
#implementation Something
+ (id)
sharedSomething
{
if (! globalSomething)
{
/* can use different initializer if necessary */
globalSomething = [[[self class] allocWithZone:NULL] init];
}
return globalSomething;
}
. . .
#end

Core Data fails to generate primitive accessors

From my understanding of Core Data, all that is necessary for primitive accessors to work is the #dynamic directive for the property name (as well as declaring primitive accessors for that property within the entity implementation).
For some reason, when using the generated primitive accessor the setState: method is not modifying the state property:
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:#"state"];
tmpValue = [self primitiveState];
[self didAccessValueForKey:#"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:#"state"];
[self setPrimitiveState:[NSNumber numberWithInt:value]];
[self didChangeValueForKey:#"state"];
}
while using the key-value-coding version does modify the state property
- (int)state
{
NSNumber * tmpValue;
[self willAccessValueForKey:#"state"];
tmpValue = [self primitiveValueForKey:#"state"];
[self didAccessValueForKey:#"state"];
return [tmpValue intValue];
}
- (void)setState:(int)value
{
[self willChangeValueForKey:#"state"];
[self setPrimitiveValue:[NSNumber numberWithInt:value] forKey:#"state"];
[self didChangeValueForKey:#"state"];
}
in both cases, I primitive accessors are declared as follows (and as per Apple's example and code generation):
#interface Post (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber *)primitiveState;
- (void)setPrimitiveState:(NSNumber *)value;
#end
I'm a bit at a loss to why this would be. Any help would be greatly appreciated!
After tremendous amounts of head-scratching, debugging, fiddling and guess-and-check, I've finally figured out what the problem is: Core Data primitive accessors AREN'T dynamically generated if you define those attributes as instance variables. I had defined them for debugging purposes (as GBD cannot see the values of properties without defined ivars, it seems), and this prevented primitive accessors from being generated correctly. This is something that Apple should really document in some form. As it's very difficult to discover on one's own. I hope this helps others who've been having the same issue!
I've been looking into this and one of the things discovered is that, contrary to docs, the implementation file generated from the data model does NOT list the primitive dynamic accessors. Other places state that you have to add them yourself. Could that be the issue?
Are you using/modifying the code of an NSManagedObject generated by Xcode? I believe that by default these are generated as "commented" out by an #if 0 directive.
Just wanted to say that I am having the same problem and had to switch to setPrimitiveValue and primitiveValueForKey based on your comment here. It bothers me that the default implementation does not work. Of note in my case is that I am subclassing another NSManagedObject. Not sure if that's your case as well.

Resources