当前位置:网站首页>[Objective-C] differences between structs and classes
[Objective-C] differences between structs and classes
2022-06-11 10:15:00 【Back end coder】
One 、 A structure can only encapsulate attributes , Classes can encapsulate not only attributes but also methods .
If 1 Encapsulated data has both properties and behaviors , Class only .
Two 、 Structure variables are allocated on the stack .OC Objects are allocated in the heap .
The stack space is relatively small . But the efficiency of data access stored in the stack is relatively high .
The space of the heap is relatively large . But the access efficiency of the data stored in the heap is relatively low .
If 1 Encapsulated data has only attributes . If the structure is used, it will be allocated on the stack Efficiency will be high .
If type is used Objects are allocated on the heap The efficiency will be relatively low .
If you define 1 Structures , There are many attributes in this structure . At this time, structural variables will occupy a large part of the stack 1 Block space It will reduce efficiency .
When to use a structure : 1. Encapsulated data has only attributes 2. Fewer properties (3 Below ).
When to use classes : 1 . Encapsulating data has both properties and behavior . 2 . Only attributes But there are many properties .
3、 ... and 、 Structure assignment is Directly assigned value . And the pointer to the object What is assigned is the address of the object .
//
// StructPractice.h
// OneLiveIOS
//
// Created by Inke219223m on 2022/5/26.
//
#import <Foundation/Foundation.h>
struct Books {
NSString *title;
NSString *author;
NSString *subject;
int book_id;
};
// Bit field
// Bit fields allow you to package data in a structure . When memory or data storage is very valuable , This is especially useful .
//struct codingce_struct {
// unsigned int f1:1;
// unsigned int f2:1;
// unsigned int f3:1;
// unsigned int f4:1;
// unsigned int type:4;
// unsigned int my_int:9;
//} codingce;
// Packaging multiple objects into machine words . for example Can be compressed 1 Bit mark . Read external file format - You can read in non-standard file formats .9 An integer .
struct codingce_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
};
NS_ASSUME_NONNULL_BEGIN
@interface StructPractice : NSObject
-(void) StructFunTest : (int) num1;
-(void) PrintStruct : (int) num1 : (struct Books) book;
@end
NS_ASSUME_NONNULL_END
//
// StructPractice.m
// OneLiveIOS
//
// Created by Inke219223m on 2022/5/26.
//
#import "StructPractice.h"
@implementation StructPractice
-(void) StructFunTest : (int) num1 {
struct Books Book1; /* Statement Book Type variable :Book1 */
struct Books Book2;
Book1.title = @" I'm lucky ";
Book1.author = @" Granting titles to gods ";
Book1.subject = @"Objective-C Programming tutorial ";
Book1.book_id = 81234566;
Book2.title = @"Java";
Book2.author = @"Maxsu";
Book2.subject = @"C Programming tutorial ";
Book2.book_id = 813283488;
/* Print Book1 Information */
NSLog(@"Book 1 title : %@\n", Book1.title);
NSLog(@"Book 1 author : %@\n", Book1.author);
NSLog(@"Book 1 subject : %@\n", Book1.subject);
NSLog(@"Book 1 book_id : %d\n", Book1.book_id);
/* Print Book2 Information */
NSLog(@"Book 2 title : %@\n", Book2.title);
NSLog(@"Book 2 author : %@\n", Book2.author);
NSLog(@"Book 2 subject : %@\n", Book2.subject);
NSLog(@"Book 2 book_id : %d\n", Book2.book_id);
[self PrintStruct: 2 : Book1];
struct Books *struct_pointer;
struct_pointer = &Book1;
NSLog(@" Pointer to structure :%@", struct_pointer->title);
}
-(void) PrintStruct : (int) num1 : (struct Books) book {
NSLog(@"book title : %@\n", book.title);
NSLog(@"book author : %@\n", book.author);
NSLog(@"book subject : %@\n", book.subject);
NSLog(@"book book_id : %d\n", book.book_id);
}
@end
边栏推荐
- steamVR简介
- Q1营收超华尔街预期,挚文集团的价值等待回归
- Interface, abstract class and relationship between classes
- ZigBee模块通信协议的树形拓扑组网结构
- MD5 learning
- 本人书签常存的地址
- 微信小程序之点餐系统附源码
- How much do you know about software compatibility testing? How to select a software compatibility testing organization?
- Browserfetcher class for getting started with puppeter
- MySQL basic learning notes 03
猜你喜欢

Q1营收超预期,满帮为何赢得逆风增长?

rpc的正确打开方式|读懂Go原生net/rpc包

吴恩达机器学习课程-第七周

Detailed explanation of Lora module wireless transceiver communication technology

Introduction to steamvr

Drink at night, 50 classic SQL questions, really fragrant~

远程工作时代的物联网安全

Q1营收超华尔街预期,挚文集团的价值等待回归

Cisp-pte XSS Foundation

BCGControlBar库专业版,完整记录的MFC扩展类
随机推荐
ZigBee模块通信协议的树形拓扑组网结构
你对软件兼容性测试知道多少?如何选择软件兼容性测试机构?
Bcgcontrolbar Library Professional Edition, fully documented MFC extension class
什么是数字孪生?一个实时而虚拟的表现形式
数据一致的问题
No more! The entry byte beat for a week and ran decisively.
转载:LinearLayout+Fragment实现下导航栏效果
Differences between beanfactory and factorybean
WordPress网站备份
Start jar
记录一下昨天的尴尬
puppeteer入门之 Browser 类
What is digital twin? A real-time and virtual representation
Database design and paradigm explanation
Pagoda panel backup and recovery data
Streaming computing knowledge
宝塔面板备份与恢复数据
Mysql比较
Dotween usage
ugui图片墙