1

This is a related question to one I posted earlier today, I was initially looking at how best to implement copyWithZone for an object that subclasses NSObject. I am pretty happy with what I have (see 001:) but wanted to ask about removing the setters (if it even matters, please say if its not really necessary).

It was pointed out to me that I could write:

newCrime->_title = [_title copyWithZone:zone];

I have two questions firstly, the -> is using C++ notation, is there an objective-c way of accessing a property of an object (without using a setter/dot notation)?

Finally, assign and strong how would I write those, I am pretty sure that assign would be:

newCrime->_coordinate = _coordinate;

but I am not sure about what to write for a strong pointer;

newCrime->_month =

.

@property(nonatomic, strong) NSString *month;
@property(nonatomic, strong) NSString *category;
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, strong) NSString *locationName;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;

.

// 001:
- (id)copyWithZone:(NSZone *)zone {
    Crime *newCrime = [[[self class] allocWithZone:zone] init];
    if(newCrime) {
        [newCrime setMonth:_month];
        [newCrime setCategory:_category];
        [newCrime setCoordinate:_coordinate];
        [newCrime setLocationName:_locationName];
        [newCrime setTitle:_title];
        [newCrime setSubtitle:_subtitle];
    }
    return newCrime;
}

1 Answer 1

2

-> is not C++ object notation, it is C pointer notation. Please get that C++ junk out of your head :)

As far as ARC is concerned, just have your variables defined in your interface as such:

@interface myObject : NSObject
{
    __strong strongIvar;
    __weak weakIvar;
    __unsafe_unretained assignIvar;
}

@end

And ARC will do the rest when you set the object using pointer notation (->).

For an example of pointer notation in pure C, look at the following example:

struct myStruct {
    int intMember;
    double doubleMember;
    char *stringMember;
};

#include <stdio.h>
#include <string.h>

int main(void) 
{
    struct myStruct *structVar = malloc(sizeof(myStruct));
    structVar->intMember = 10;
    structVar->doubleMember = M_PI * 2;
    structVar->stringMember = strdup("Hello World!");

    printf("%i %d %s", structVar->intMember, structVar->doubleMember, structVar->stringMember);

    free(structVar->stringMember);
    free(structVar);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, my apologies with regards to C++, I was referring to -> in the OOP sense of accessing a member variable of an object. If you specify the variables as __strong __weak etc. in the @interface is it then sufficient to just directly assign them afterwards i.e. newCrime->_month = _month; ?
@fuzzygoat yes, ARC will add retain / release cycles as necessary. But note that copy properties will need to be strong and assigned by using the -copy selector.
Thank you, much appreciated Richard, I am guessing that with respect to my copyWithZone implementation that I would want to assign copy properties using -copyWithZone?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.