当前位置:网站首页>[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
边栏推荐
- MySQL comparison
- 《1.图书馆里系统》
- Empire CMS imitation "game bird" template source code /92kaifa version large mobile game portal template
- What is the difference between an interface and an abstract class?
- 启动jar
- MySQL 权限管理和备份
- Finish C language
- 不卷了!入职字节跳动一周就果断跑了。
- Record yesterday's embarrassment
- [Bert]: Calculation of last ave state when training tasks with similar Bert semantics
猜你喜欢

浅谈wandfluh比例阀的功能与作用

DENISON液压泵用什么液压油?有哪些要求

With determination to forge ahead, JASMINER continues to deepen its brand strength

ugui图片墙

Initial deployment of Servlet

ESP8266_ SNTP(Simple Network Time Protocol)

Detailed explanation of Lora module wireless transceiver communication technology

Some code fragments of a universal and confession wall system developed by PHP

Reprint: linearlayout+fragment to achieve the lower navigation bar effect

Secret behind the chart | explanation of technical indicators: tangqi'an channel
随机推荐
What are the functions and applications of Mogg test controller
steamVR简介
General idea of interface tuning
Technology cloud report: privacy computing under the wave of Web3.0
VK2C22A/B抗干扰系列电表,水表段码LCD液晶驱动芯片资料(可定制DICE/COG)
接口调优的大致思路
What are the differences between Parker pilot solenoid valve and direct acting solenoid valve?
数据库设计及范式讲解
帝国CMS仿《手艺活》DIY手工制作网源码/92kaifa仿手艺活自适应手机版模板
外包干了四年,废了...
Q1营收超预期,满帮为何赢得逆风增长?
puppeteer入门之 Puppeteer 类
[image denoising] image denoising based on median + wavelet + Wiener + filter, including Matlab source code
After four years of outsourcing, it was abandoned
Wechat applet ordering system with source code
UGUI
Internet of things security in the era of remote work
浅谈wandfluh比例阀的功能与作用
图片规则翻页
锐意进取,砥砺前行,JASMINER持续深耕品牌实力