ios-set和get方法

在OC的类中,我们如果在interface{}中定义了成员变量,如果我们想要在类外给他们赋值和获取他们的值,我们就需要给他们写set方法和get方法,否则会报错。如果成员变量名是age,对于set方法的格式要求是setAge,意思就是说set方法要以set开头后面的字符为变量的大写字母开头,get方法就是一般就是以变量名来命名。比如说

#import <Foundation/Foundation.h>

@interface RectMno : NSObject
{
    @public NSInteger age;
}
@property (nonatomic,assign) int a;
-(int)age;
-(void)setAge:(int)age1;
@end
#import "RectMno.h"

@implementation RectMno
-(void)setAge:(int)age1
{
    age=age1;
}
-(int)age
{
    return age;
}
@end
只有给这个成员变量定义了get方法和set方法才能得到它的值和获取它的值点运算符在oc的类对象中不能访问成员变量,如rect.age=5,这里不是使用成员变量age,而是调用成员方法setAge,相当于给rect.age赋值,调用方法[rect setAge:5]; NSLog(@" %d ", rect.age); 这里也不是使用成员变量age,而是调用成员方法age, 使用dog.age表达式的值时,实际上就是调用成员方法age,  [rect  age].  " . " 不能调用成员变量,只是调用set函数和get函数的一种简写。 这里不能理解为定义了get函数和get函数就能调用成员变量.

而我们如果定义了一个属性就会自动生成get方法和set方法。但是如果同时重写了这个属性的get方法和set方法就会报错,单独重写任何一个都不会报错。主要是因为当你复写了get和set方法之后@property默认生成的@synthesize就不会起作用了,这也就意味着你的类不会自动生成出来实例变量了,你就必须要自己声明实例变量,比如说你定义了一个属性

@property (nonatomic, copy) NSString *name;
则在interface{}中就要定义一个NSString *_name;,否则会报错。因为一旦同时重写了setter、getter方法,并且没有实现@synthesizer,@synthesizer就不再生成实例变量。

属性的作用是生成setter以及getter方法的实现,如果方法内部操作的实例变量未定义,系统会自动生成一个_属性名的实例变量,但是生成的实例变量的可见度是私有的,子类不可访问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在iOS项目中添加OpenSSL库并导入头文件。可以使用CocoaPods来安装OpenSSL,也可以手动下载并添加到项目中。 在使用OpenSSL加密之前,需要先初始化库: ``` #include <openssl/evp.h> EVP_CIPHER_CTX *ctx; ctx = EVP_CIPHER_CTX_new(); ``` 接下来,我们可以使用EVP接口实现AES-GCM加密。以下是一个示例代码: ``` #include <openssl/rand.h> void aes_gcm_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad, int aad_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char *ciphertext, unsigned char *tag) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; // Create and initialise the context if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); // Initialise the encryption operation. if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL)) handleErrors(); // Set IV length if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL)) handleErrors(); // Set tag length if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, NULL)) handleErrors(); // Initialise key and IV if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors(); // Provide any non-AAD data if (aad_len > 0) { if (1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len)) handleErrors(); } // Provide the message to be encrypted, and obtain the ciphertext if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleErrors(); ciphertext_len = len; // Finalise the encryption if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); ciphertext_len += len; // Get the tag if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) handleErrors(); // Clean up EVP_CIPHER_CTX_free(ctx); } ``` 在此示例中,我们使用EVP_aes_128_gcm()进行AES-GCM加密。我们还提供了一个AAD(Additional Authenticated Data)参数,它是与加密数据一起传输的附加信息。最后,我们获得了加密后的密文和GCM标签。 下面是一个使用ECB模式进行加密的示例代码: ``` void aes_ecb_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; // Create and initialise the context if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); // Initialise the encryption operation. if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL)) handleErrors(); // Provide the message to be encrypted, and obtain the ciphertext if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleErrors(); ciphertext_len = len; // Finalise the encryption if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); ciphertext_len += len; // Clean up EVP_CIPHER_CTX_free(ctx); } ``` 在此示例中,我们使用EVP_aes_128_ecb()进行ECB加密。 这里的示例仅供参考,实际实现过程中需要根据具体情况进行调整。同时,为了保护数据安全,建议在使用OpenSSL时遵循最佳安全实践,例如使用随机生成的IV、密钥和盐值等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值