OBJECTIVE-C OC Syntax: Member of struct property of an object cannot be modified

Source: Internet
Author: User
Tags uikit

Look at the topic and know what this article is written about.

BackBtn.frame.size = cgmakesize (+);  This is the wrong way to do it.

In OC, a member of the struct property of an object is not allowed to be modified, the above statement, BACKBTN is an object that we instantiate, frame is its struct property, and size is the member variable of the property of the frame. According to OC syntax, we know that the above code is wrong. If you want to set size, we do this:

CGRect frame = backbtn.frame;
Frame.size = cgmakesize (+);
<pre name= "code" class= "OBJC" >backbtn.frame = frame;


The solution above is to assign the structure property of the original object to a temporary structure, then modify the members in the temporary structure, and then assign the modified struct variable directly to the structure variable of the original object, which is equivalent to the modification.

But we know that there are many such members, such as Origin in frame, x in Size,origin, width in y,size, height. Each member of us is very troublesome to assign, so I suggest you add a category (category) so that each object's attributes can be directly assigned, and the tedious statements of the assignment are written in category.




#import <UIKit/UIKit.h>

@interface UIView (Extension)

@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) cgfloat width;
@property (nonatomic, assign) cgfloat height;
@property (nonatomic, assign) cgsize size;
@property (nonatomic, assign) Cgpoint origin;

@end

#import "Uiview+extension.h" @implementation UIView (Extension)//Override set Method-(void) SetX: (cgfloat) x {cgrect frame =
    Self.frame;
    frame.origin.x = x;
Self.frame = frame;
    }-(void) Sety: (cgfloat) y {cgrect frame = self.frame;
    FRAME.ORIGIN.Y = y;
Self.frame = frame;
    }-(void) SetWidth: (cgfloat) Width {cgrect frame = self.frame;
    Frame.size.width = width;
Self.frame = frame;
    }-(void) SetHeight: (cgfloat) Height {cgrect frame = self.frame;
    Frame.size.height = height;
Self.frame = frame;
    }-(void) SetSize: (cgsize) Size {CGRect frame = self.frame;
    frame.size = size;
Self.frame = frame;
    }-(void) Setorigin: (Cgpoint) origin {CGRect frame = self.frame;
    Frame.origin = origin;
Self.frame = frame;

}//Override Get Method-(CGFloat) x {return self.frame.origin.x;}

-(CGFloat) y {return self.frame.origin.y;}

-(CGFloat) width {return self.frame.size.width;}

-(cgfloat) height {return self.frame.size.height;} - (cgsize) Size {return self.frame.size;}
 -(Cgpoint) origin {return self.frame.origin;} @end

Add: Only write header file is also possible, I think it should be @property automatically generated the corresponding get and set method. This is debatable.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.