当前位置:网站首页>[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

原网站

版权声明
本文为[Back end coder]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110924148851.html