Search Results

Search found 10 results on 1 pages for 'nscopying'.

Page 1/1 | 1 

  • Best practice for copying private instance vars with NSCopying

    - by Ben
    I might be missing something obvious here, but I'm implementing NSCopying on one of my objects. That object has private instance variables that are not exposed via getters, as they shouldn't be used outside the object. In my implementation of copyWithZone:, I need alloc/init the new instance, but also set up its state to match the current instance. I can obviously access current private state from inside copyWithZone:, but I can't set it into the new object, because there are no accessors for that state. Is there a standard way around this while still keeping data privacy intact? Thanks.

    Read the article

  • UIView as dictionary key?

    - by subw
    I want to have a NSDictionary that maps from UIViews to something else. However, since UIViews do not implement the NSCopying protocol, I can't use them directly as dictionary keys.

    Read the article

  • Proper way to copy a readonly NSMutableArray

    - by Jon Hull
    I have an object with a readonly property that I am trying to implement NSCopying for. It has a mutableArray called "subConditions" (which holds "SubCondition" objects). I have made it readonly because I want callers to be able to change the data in the array, but not the array itself. This worked really well until it was time to write the -copyWithZone: method. After fumbling around a bit, I managed to get something that seems to work. I am not sure if it is the best practice though. Here is a simplified version of my -copyWithZone: method: -(id)copyWithZone:(NSZone*)zone { Condition *copy = [[[self class]allocWithZone:zone]init]; NSArray *copiedArray = [[NSArray alloc]initWithArray:self.subConditions copyItems:YES]; [copy.subConditions setArray:copiedArray]; [copiedArray release]; return copy; } Is this the correct/best way to copy a readonly mutableArray?

    Read the article

  • Are protocols inheritable in Objective-C?

    - by aquaibm
    I saw this in some header file in the framework directory: @interface NSCharacterSet : NSObject <NSCopying, NSMutableCopying, NSCoding> @end @interface NSMutableCharacterSet : NSCharacterSet <NSCopying, NSMutableCopying> @end I thought protocols were inheritable.If I am right about that,There is no need to type <NSCopying, NSMutableCopying> again after "NSMutableCharacterSet : NSCharacterSet".And NSMutableCharacterSet also conforms to NSCoding protocol, right? Than why is Apple typing that again?Am I making mistake?

    Read the article

  • Memory leak with objective-c on alloc

    - by Grunzig
    When I use Instruments to find memory leaks, a leak is detected on Horaires *jour; jour= [[Horaires alloc] init]; // memory leak reported here by Instruments self.lundi = jour; [jour release]; and I don't know why there is a leak at this point. Does anyone can help me? Here's the code. // HorairesCollection.h #import <Foundation/Foundation.h> #import "Horaires.h" @interface HorairesCollection : NSObject < NSCopying > { Horaires *lundi; } @property (nonatomic, retain) Horaires *lundi; -init; -(void)dealloc; @end // HorairesCollection.m #import "HorairesCollection.h" @implementation HorairesCollection @synthesize lundi; -(id)copyWithZone:(NSZone *)zone{ DefibHoraires *another = [[DefibHoraires alloc] init]; another.lundi = [lundi copyWithZone: zone]; [another autorelease]; return another; } -init{ self = [super init]; Horaires *jour; jour= [[Horaires alloc] init]; // memory leak reported here by Instruments self.lundi = jour; [jour release]; return self; } - (void)dealloc { [lundi release]; [super dealloc]; } @end // Horaires.h #import <Foundation/Foundation.h> @interface Horaires : NSObject <NSCopying>{ BOOL ferme; BOOL h24; NSString *h1; } @property (nonatomic, assign) BOOL ferme; @property (nonatomic, assign) BOOL h24; @property (nonatomic, retain) NSString *h1; -init; -(id)copyWithZone:(NSZone *)zone; -(void)dealloc; @end // Horaires.m #import "Horaires.h" @implementation Horaires -(BOOL) ferme { return ferme; } -(void)setFerme:(BOOL)bFerme{ ferme = bFerme; if (ferme) { self.h1 = @""; self.h24 = NO; } } -(BOOL) h24 { return h24; } -(void)setH24:(BOOL)bH24{ h24 = bH24; if (h24) { self.h1 = @""; self.ferme = NO; } } -(NSString *) h1 { return h1; } -(void)setH1:(NSString *)horaire{ [horaire retain]; [h1 release]; h1 = horaire; if (![h1 isEqualToString:@""]) { self.h24 = NO; self.ferme = NO; } } -(id)copyWithZone:(NSZone *)zone{ Horaires *another = [[Horaires alloc] init]; another.ferme = self.ferme; another.h24 = self.h24; another.h1 = self.h1; [another autorelease]; return another; } -init{ self = [super init]; return self; } -(void)dealloc { [h1 release]; [super dealloc]; } @end

    Read the article

  • Unable to use 'class' as a key in NSDictionary with Xcode 4.5

    - by Vivin Paliath
    I'm trying to use a class as a key in an NSDictionary. I looked at the answer to this question and what I have is pretty much the same; I'm using setObject: forKey:. However, XCode complains, saying Incompatible pointer types sending 'Class' to parameter of type 'id<NSCopying>'. The call I have is: [_bugTypeToSerializerDictionary setObject: bugToStringSerializer forKey: [bugToStringSerializer serializedObjectType]]; bugToStringSerializer is an instance of BugToStringSerializer whose concrete implementations implement serializedObjectType. An example of a concrete implementation looks like this: - (Class) serializedObjectType { return [InfectableBug class]; } What am I doing wrong here?

    Read the article

  • Object pointer value as key into dictionary

    - by Ranking Stackingblocks
    I want to use the object's reference value as a key into a dictionary, as opposed to a copy of value of the object. So, I essentially want to store an object associated with a particular instance of another object in a dictionary and retrieve that value later. Is this possible? Is it completely against the idea of NSDictionary? I can tell that I am probably approaching this the wrong way because the dictionary wants me to implement NSCopying on the object itself, which doesn't really make sense in terms of what I'm doing. I can see that what I should really be doing is wrapping the pointer value, but that seems a little mad. Advice would be appreciated.

    Read the article

  • Couldn't I just pass an copied string to an Core Data property?

    - by dontWatchMyProfile
    The docs say: The default implementation does not copy attribute values. If the attribute value may be mutable and implements the NSCopying protocol (as is the case with NSString, for example), you can copy the value in a custom accessor to help preserve encapsulation (for example, in the case where an instance of NSMutableString is passed as a value). So instead of getting into trouble and inconvenience with overwriting accessors in my NSManagedObject subclass, couldn't I simply do something like this? myManagedObject.firstName = [[firstNameMutableStr copy] autorelease]; This would have the exact same effect, or not? The dynamic implementation would retain that anyways ... so.... why not the easy way?

    Read the article

  • How to get multiple copies of a UIView loaded from a nib?

    - by dmaestro12
    I want to use a UIView heirarchy multiple times (the nib object is a template). Unfortunately, UIView does not conform to <NSCopying so [cell.contentView addSubview: [[templEditCellView copy] autorelease]]; does not work. I wasn't surprised, since I want a deep copy of the view heirarchy. Currently the view is a top-level object in the nib it is loaded from. Is there a way to reload a single specified top-level object from the nib? Should I split out the view to a single NIB which can be reloaded on demand? Or is there another way to make a deep copy of a view? Thanks!

    Read the article

1