Last active
December 16, 2015 06:09
-
-
Save romyilano/5389460 to your computer and use it in GitHub Desktop.
review of Structs Creating iOS 5 Apps, Develope and design
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Structs are the most flexible C data type | |
| can combine different types of data, access that data using named fields | |
| makes heavy use of C Structures -- many Core Frameworks in ObjC are written in pure C | |
| */ | |
| struct CGPoint { | |
| CGFloat x; | |
| CGFloat y; | |
| }; | |
| typedef struct CGPoint CGPoint; // alias for CGPoint struct | |
| /* 1. Framework creates structure called CGPoint | |
| * Structure has 2 fields, x and y | |
| * Both fields happen to be the same data type (CGFloat) | |
| */ | |
| // the typedef enables us to avoid constantly referring to the entity as "struct CGPoint" in our code. | |
| // now we can ddrop struct and treat it like any other data type | |
| CGPoint pixelA; // creates CGPoint variable | |
| pixelA.x = 23.5; // sets the x field | |
| pixelA.y = 32.6; // sets the y field | |
| int x = pixelA.x; // reads the value from the x field | |
| // for any struct type Apple provides convenience method for creating the struct, a method for comparing structs, methods to perform common | |
| // operations with the struct. for CGPoint these include CGPointMake(), CGPointEqualToPoint() and CGRectContainsPoint() | |
| // 3 approaches to creating a CGRect. all equivalent | |
| CGRect r1; | |
| r1.origin.x=5; | |
| r1.origin.y=10; | |
| r1.size.width=10; | |
| r1.size.height=20; | |
| CGRect r2 = CGRectMake(5, 10, 10, 20); | |
| // struct literal - each pair of curly braces represents a struct | |
| CGRect r3 = {{5,10}, {10,20}}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment