copying a custom object

前端 未结 1 881
青春惊慌失措
青春惊慌失措 2021-01-25 03:29

I have an object called Layer that has some attributes and some methodes.

i need to pass Layer to a second view controller:

SecondVC *view = [self.storyb         


        
1条回答
  •  温柔的废话
    2021-01-25 03:56

    You will need to implement copy function to your object

    In your Layer.m

    - (id)copy
    {
        Layer *layerCopy = [[Layer alloc] init];
    
        //YOu may need to copy these values too, this is a shallow copy
        //If YourValues and someOtherValue are only primitives then this would be ok
        //If they are objects you will need to implement copy to these objects too
        layerCopy.YourValues = self.YourValues;
        layerCopy.someOtherValue = self.someOtherValue;
    
        return layerCopy;
    }
    

    Now in your calling function

    //instead of passing self.Layer pass [self.Layer copy]
    view.Layer = [[Layer alloc] initWithMapLayer:[self.Layer copy]];
    

    0 讨论(0)
提交回复
热议问题