@synthesize的使用

利用@synthesize可以给在.m文件中给.h文件中的属性重新定义新的名称如
@synthesize  firstname = anothername;firstname是在.h文件中定义的,新定义的属性名称为anothername在.m文件中使用属性anothername时,就相当于使用firstname属性,对anothername进行赋值也相当于对firstname属性进行赋值。

注意:重新定义的属性名称只能在.m文件中使用,在其他文件无法使用这个属性名,只能使用原来的属性名。
时间: 2024-08-07 17:02:38

@synthesize的使用的相关文章

如果将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法

如果将synthesize省略,并且我们自己实现setter和getter方法时,系统就不会生成对应的setter和getter方法,还有实例变量 1,当把语义特性声明为assign时,setter和getter时方法内部实现 - (void)setName:(NSString *)name{ _name = name; } - (NSString *)name{ return _name; } 2,当把语义特性声明为retain时,setter和getter方法内部实现 - (void)set

@property @synthesize 对着set方法和get方法

@property @synthesize 这两个的作用是代替set和get方法: @property int age;代替下面两句 - (void)setAge:(int)age; - (int)age; 作用: 自动生成某个成员变量的setter和getter声明: @property double weight; @property NSString *name; 现在需要到实现中去实现: 使用@synthesize age;会把age property这些声明进行实现: @synthes

OC语言@property @synthesize和id

一.@property @synthesize 这两个关键字是编译器特性,让xcode可以自动生成gettersetter的声明和实现. (一)@property  @property 关键字可以自动生成某个成员变量的settergetter方法的声明 @property int age; 编译时遇到这一行,则自动扩展成下面两句: - (void)setAge:(int)age; - (int)age; (二)@synthesize @synthesize关键字帮助生成成员变量的setterge

Objective-C基础笔记(2)@property和@synthesize

先贴出使用@property和@synthesize实现的上一篇中的代码,再解释这两个关键字的用法和含义,代码如下: Person.h文件 #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; //可以被子类访问 //这里系统会帮我们生成一个默认的 int _no 私有变量(不能被子类访问) } @property int age; @property int no; //自己写一个构造方法 - (

黑马程序员 [email&#160;protected]和@synthesize

———Java培训.Android培训.iOS培训..Net培训.期待与您交流! ——— 1.@property 和@synthesize 的作用 (1) 通过@property可以自动生成属性的set,get方法的声明部分 生成的是set,get方法是哪个属性的,@property后面的名称就是属性去掉下划线后的部分 例如: - (void)setName: (NSString *)name; - (NSString *)name; 这两行代码可以用 @property (NSString *

@synthesize和@dynamic分别有什么作用?

synthesize,编译器自动生成setter和getter的方法,在你没有手动去实现这两个方法时.dynamic,告诉编译器你会动态生成setter和getter方法,不会要编译器帮你生成. @synthesize date=_date;这个一直很迷惑人,这个是帮你的属性绑定一个成员变量. 现在已经不需要写synthesize.

@synthesize、 @dynamic 的使用方法

在声明property属性后,有2种实现选择 @synthesize 编译器期间,让编译器自动生成getter/setter方法. 当有自定义的存或取方法时,自定义会屏蔽自动生成该方法 @dynamic 告诉编译器,不自动生成getter/setter方法,避免编译期间产生警告 然后由自己实现存取方法 或存取方法在运行时动态创建绑定:主要使用在CoreData的实现NSManagedObject子类时使用,由Core Data框架在程序运行的时动态生成子类属性 @dynamic这个关键词,通常是

Objective-C中的@property和@synthesize用法

@代表“Objective-C”的标志,证明您正在使用Objective-C语言 Objective-C语言关键词,@property与@synthesize配对使用. 功能:让编译好器自动编写一个与数据成员同名的方法声明来省去读写方法的声明. 如: 1.在头文件中: C代码   @property int count; 等效于在头文件中声明2个方法: C代码   - (int)count; -(void)setCount:(int)newCount; 2.实现文件(.m)中 C代码   @sy

property、synthesize、id

1.@property int age; 在编译器情况下会自动编译展开为: <age在setter中首字母大写,点语法为p.age> - (void)setAge:(int)age; - (int)age;   2.同理:@property int _age; 在编译器情况下会自动编译展开为: <_age在setter中首字母大写,为横线,大写仍保持,此时点语法为p._age> - (void)set_age:(int)age; - (int)_age; 一般情况,使用1,而不使用