//C语言风格的枚举
typedef enum : NSUInteger {
RJDemoTypeA,
RJDemoTypeB,
RJDemoTypeC,
} RJDemoType;
//OC语言风格的枚举
typedef NS_ENUM(NSUInteger, RJType) {
RJTypeA,
RJTypeB,
RJTypeC,
};
//位移枚举
typedef NS_OPTIONS(NSUInteger, RJActionType) {
RJActionTypeTop = 1 << 0, //1
RJActionTypeBottom = 1 << 1, //2
RJActionTypeLeft = 1 << 2, //4
RJActionTypeRight = 1 << 3, //8
};
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self RJTypeGo:RJTypeB];
[self RJActionTypeGo:RJActionTypeTop | RJActionTypeLeft];
}
-(void)RJTypeGo:(RJType)type{
switch (type) {
case RJTypeA: NSLog(@"RJTypeA"); break;
case RJTypeB: NSLog(@"RJTypeB"); break;
case RJTypeC: NSLog(@"RJTypeC"); break;
default: break;
}
}
-(void)RJActionTypeGo:(RJActionType)type{
if (type & RJActionTypeTop) {
NSLog(@"RJActionTypeTop");
}
if (type & RJActionTypeBottom) {
NSLog(@"RJActionTypeBottom");
}
if (type & RJActionTypeLeft) {
NSLog(@"RJActionTypeLeft");
}
if (type & RJActionTypeRight) {
NSLog(@"RJActionTypeRight");
}
}
//log....
RJTypeB
RJActionTypeTop
RJActionTypeLeft
左移 | << |
右移 | >> |
按位或 | ︳ |
按位并 | & |
按位取反 | ~ |
按位异或 | ^ |
& 操作(按位并:同真则真)
0 0 1 0 1 1 1 0 46
1 0 0 1 1 1 0 1 157
———————————————
0 0 0 0 1 1 0 0 12
| 操作(按位或:一真则真)
0 0 1 0 1 1 1 0 46
1 0 0 1 1 1 0 1 157
———————————————
1 0 1 1 1 1 1 1 191
~ 操作(按位取反:真为假 假为真)
0 0 1 0 1 1 1 0 46
———————————————
1 1 0 1 0 0 0 1 209
^ 操作(按位异或: 一真为真 真真假假都为假)
0 0 1 0 1 1 1 0 46
1 0 0 1 1 1 0 1 157
———————————————
1 0 1 1 0 0 1 1 179
C语言用法
// 1.先定义结构体类型, 在定义结构体变量
struct RJPersonStudent {
char *name;
int age;
double height;
};
// 2.定义结构体类型的同时定义结构体变量
struct RJPersonStudent2 {
char *name;
int age;
double height;
} ps2;
// 3.定义结构体类型的同时定义结构体变量, 并且省略结构体名称
struct {
char *name;
int age;
double height;
} ps3;
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 特别注意: 结构体和数组有一点区别, 数组不能先定义再进行一次性的初始化, 而结构体可以
// 只不过需要明确的告诉系统{}中是一个结构体
// C数组: 是用于保存一组相同类型的数据
// 结构体: 是用于保存一组不同类型的数据
// 1.定义的同时初始化
struct RJPersonStudent xiaoming = {"xiaoming",13,150.5};
// 2.先定义再初始化(逐个初始化)
struct RJPersonStudent lihua;
lihua.name = "lihua";
lihua.age = 12;
lihua.height = 145.9;
// 3.先定义再初始化(一次性初始化)
struct RJPersonStudent lili;
lili = (struct RJPersonStudent){"lili", 18, 172.5}; // 数组? 结构体?
ps2.name = "xiaohua";
ps3.name = "142857";
struct RJPersonStudent2 ps2_xiaoxia;
ps2_xiaoxia.name = "psxiaoxia";
NSLog(@"xiaoming.name = %s",xiaoming.name);
NSLog(@"xiaoming.age = %d",xiaoming.age);
NSLog(@"xiaoming.height = %f",xiaoming.height);
NSLog(@"ps2.name = %s",ps2.name);
NSLog(@"ps3.name = %s",ps3.name);
NSLog(@"ps2_xiaoxia.name = %s",ps2_xiaoxia.name);
//定义一个指向结构体的指针
struct RJPersonStudent2 *ips2;
ips2 = &ps2; //问结构体方式一: 结构体指针变量名称->属性
(*ips2).age = 16;//问结构体方式二: (*结构体指针变量名称).属性
ips2->name = "Zhi_Zhen_Fang_Wen";
NSLog(@"ips2.name = %s",ips2->name);
NSLog(@"ips2.name = %d",(*ips2).age);
}
xiaoming.name = xiaoming
xiaoming.age = 13
xiaoming.height = 150.500000
ps2.name = xiaohua
ps3.name = 142857
ps2_xiaoxia.name = psxiaoxia
ips2.name = Zhi_Zhen_Fang_Wen
ips2.name = 16
OC中的用法
---仿CGRectMake
struct RJSize {
CGFloat width;
CGFloat height;
};
typedef struct CG_BOXABLE RJSize RJSize;//给结构体定义别名
struct RJPoint {
CGFloat x;
CGFloat y;
};
typedef struct CG_BOXABLE RJPoint RJPoint;//给结构体定义别名
struct RJRect {
RJPoint origin;
RJSize size;
};
typedef struct CG_BOXABLE RJRect RJRect;//给结构体定义别名
//CG_INLINE 是一个宏:#define CG_INLINE static inline
//inline 内联函数:即在编译的时候将函数体替换函数调用,从而不需要将parameter,return address进行push/pop stack的操作,从而加速app的运行,然而,会增加二进制文件的大小。
//static inline RJRect
CG_INLINE RJRect
RJRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height){
RJRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
UIKIT_EXTERN NSString *NSStringFromRJRect(RJRect rect);
const NSString *NSStringFromRJRect(RJRect rect){
return [NSString stringWithFormat:@"{%@,%@},{%@,%@}",@(rect.origin.x),@(rect.origin.y),@(rect.size.width),@(rect.size.height)];
}
NSStringFromRJRect-------{10.23,20.368313123},{100,50}
swift中的用法
// 1.定义结构体
struct Location {
var x : Double
var y : Double
}
/* 2.对结构体的构造函数进行扩充
1.默认情况下通过Location(x: 0, y: 0)方式去创建结构体时,是在调用系统给结构体的提供的一个构造函数
2.在一个构造函数执行结束时,是必须保证所有的成员变量都已经被初始化
3.如果扩充了构造函数,并且没有明确的实现系统默认的构造函数,那么扩充的构造函数会覆盖系统默认的构造函数
*/
struct Location2 {
var x : Double
var y : Double
init(x : Double, y : Double) {
self.x = x
self.y = y
}
init(x : String, y : String) {
self.x = Double(x)!
self.y = Double(y)!
}
init() {
x = 0
y = 48
}
}
// 3.给自定义的结构体扩充方法
struct Location3 {
var x : Double
var y : Double
// 给结构体扩充函数,必须在函数前加 mutating(转变)
mutating func moveX(distance : Double) {
x += distance
}
mutating func moveY(distance : Double) {
y += distance
}
mutating func test() {
print("给自定义的结构体扩充方法")
}
}
// 4.给系统的结构体扩充方法 extension(扩展)
extension CGPoint {
mutating func moveX(distance : CGFloat) {
x += distance
}
}
let location = Location(x: 100, y: 200)
print("location----",location)
let location2 = Location2()
print("location2---",location2)
var location3 = Location3(x: 88, y: 99)
location3.moveX(distance: 100)
location3.moveY(distance: -100)
print("location3---",location3)
var point = CGPoint(x: 100, y: 100)
point.moveX(distance: 50)
print(point)
location---- Location(x: 100.0, y: 200.0)
location2--- Location2(x: 0.0, y: 48.0)
location3--- Location3(x: 188.0, y: -1.0)
point--- (150.0, 100.0)
网友评论