`
helpbs
  • 浏览: 1166286 次
文章分类
社区版块
存档分类
最新评论

objective-C中的description方法

 
阅读更多

objective-C是C语言的一个扩展集,但是它是一门面向对象语言。如果学过C#或是java的朋友应该知道在C#的类中有一个Tostring()方法,在objective—C中也有一个类似的方法,即description()方法。

description方法是基类NSObject 所带的方法,在自定义的类中,我们可以重载改方法。例如:我们自定义一个people类:

@interface People:NSObject

{

NSString *name;

int age;

}

@ implementation People

{

-(id) init

{

if(self=[super init])

{

name="老三";

age=30;

}

}

}

@end

然后,实例化people类:People *laosan=[People new];

如果我们想看看laosan这个实例的具体信息,我们可以调用,description方法,但是people类里面又没有定义,所以只能调用基类,即NSObject 中的方法。但是这样,得到的信息又和我们所想要的不一样,所以我们必须定义自己的的description方法。即:

-(NSString *)description()

{

return [NSString stringWithFormat:@"姓名: %@ 年龄: %d",name,age];

}

这样,我们再次调用description时输出便是我们想要得到的信息。

分享到:
评论

相关推荐

    Objective-C的语法与Cocoa框架

    7. Objective-C的description方法 8. Objective-C的异常处理 9. id类型 10. 类的继承 11. 动态判定与选择器 12. 类别Category 13. 协议@protocol 14. 内存管理 15. 常用的类型 16. 写入和读取属性 17. 对象的复制 18...

    Objective-C课大三作业.rar

    内容概要:这份作业包含了多个Objective-C案例,涵盖了成员变量的引用、set与get方法、继承、多态、self关键字、点语法、init方法重写、自定义构造方法、description方法、野指针错误、autorelease和自动释放池、...

    iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics

    iOS 7 Programming Fundamentals: Objective-C, Xcode, and Cocoa Basics by Matt Neuburg (Author) Publisher: O’Reilly Media (October 2013) Language: English ISBN-10: 1491945575 ISBN-13: 978-...

    Programming in Objective-C, 6th Edition

    Book Description Updated for OS X 10.9 Mavericks, iOS 7, and Xcode 5 Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming ...

    传智博客-Objective-C PPT

    本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等

    Programming in Objective-C 4th Edition

    An Objective-C Class for Working with Fractions 30 The @interface Section 33 Choosing Names 34 Class and Instance Methods 35 The @implementation Section 37 The program Section 39 Accessing Instance ...

    Objective-C-Fake-Code:Objective-C 伪代码

    Objective-C-Fake-Code Objective-C 伪代码1. 语言和运行时1.1 为枚举类型创建字符串表示 NSString * const UITableViewCellStyleDescription[] = { [UITableViewCellStyleDefault] = @" Default " ,...

    Objective-CUPS:通用Unix打印系统的Objective-C框架

    ### Objective-C框架,用于与CUPS进行交互。 OCPrinter对象符合NSSecureCoding的要求,可与NSXPC服务和特权帮助程序工具一起使用,以便非管理员用户可以自己管理打印机。 ####添加/删除OCPrinter NSError *error...

    NSDictionary-Introspect:Objective-C 运行时属性自省

    Objective-C 运行时属性自省 以 NSDictionary 的形式按需打印出类的属性和值。 有关更多信息,请阅读: : 安装 是推荐的安装NSDictionary-Introspect 。 只需Podfile添加到您的Podfile : 播客文件 pod '...

    SSXboxLeaders:XboxLeaders.com Objective-C API包装器

    SSXboxLeaders是一个基于块的Objective-C类,包装了xboxleaders.com API,以便在IOS和OS X项目中更轻松地使用。 包含一个演示应用程序,用于显示库的基本功能。 这是非常基础的,几乎没有错误检查也没有GUI更新,...

    Stubbilino:Objective-C 的简单存根

    Objective-C 的简单存根 NSObject <SBStub> *stub = [Stubbilino stubObject: [[ NSObject alloc ] init ]]; [stub stubMethod: @selector ( description ) withBlock: ^{ return @" Stubbilino is awesome! " ; }...

    RCBacktrace:获取Objective-C和Swift的任何线程的回溯

    获取用于Objective-C和Swift的任何线程的回溯。 只有少量的C代码,几乎所有代码都是在Swift中完成的。 它比Thread.callStackSymbols功能强大得多,callStackSymbols只能获取当前线程调用堆栈符号,而该符号不能在...

    BCSqliteORM_FMDB:基于FMDB +运行时的Objective-C ORM

    BCSqliteORM v1.0 基于FMDB( )和Objective-C运行时的Objective-C ORM。用法设置我正在使用作为SQLite包装器。实施BCORMEntityProtocol 使您的模型实体实现BCORMEntityProtocol协议@interface ClassEntity : ...

    dsdump:改进的nm + Objective-C和Swift类转储

    在Objective-C类上效果很好 ...和Swift类型 男人 dsdump(1) BSD General Commands Manual dsdump(1) NAME dsdump -- An improved nm + objc/swift class-dump SYNOPSIS dsdump [option...] DESCRIPTION ...

    Resort:用于在Objective-C中排序的简单比较器库

    通过compare:方法为Foundation中已经支持顺序的类定义了顺序compare:方法: NSString , NSDate , NSNumber , NSIndexPath以及它们的子类,例如NSMutableString和NSDecimalNumber 。 在其他情况下,比较器仅对...

    Head First iPhone and iPad Development, 3rd Edition

    A Learner’s Guide to Creating Objective-C Applications for the iPhone and iPad Book Description : Let’s say you have a killer app idea for iPhone and iPad. Where do you begin? Head First iPhone and...

    iPhone in Action: Introduction to Web and SDK Development

    Proficiency with C, Cocoa, or Objective-C is helpful but not required. What's Inside A comprehensive tutorial for iPhone programming Web development, the SDK, and hybrid coding Over 60 web, Dashcode...

    iOS编程教程,第2版

    简介: Book Description Get a rapid introduction to iPhone, iPad, and iPod touch programming. With this easy-to-follow guide, you’ll learn the steps necessary for developing your first marketable iOS...

    Developing Hybrid Applications for the iPhone: Using HTML, CSS, and JavaScript to Build Dynamic Apps for the iPhone

    “For those not ready to tackle the complexities of Objective-C, this is a great way to get started building iPhone apps. If you know the basics of HTML, JavaScript, and CSS, you’ll be building apps ...

    SwiftTryCatch:添加对Swift的try-catch支持

    标志): import SwiftTryCatch或导入到Obj-C桥接头中: #import "SwiftTryCatch.h"用SwiftTryCatch.try({ // try something }, catch: { (error) in print("\(error.description)") }, finally: { // close resou

Global site tag (gtag.js) - Google Analytics