Ry’s Objective-C Tutorial---Objective-C Data Types

RyPress - Quality Software Tutorials

You’re reading Ry’s Objective-C Tutorial

Objective-C Data Types

Objective-C inherits all of the primitive types of C, and defines a few of its own, too. But, applications also require higher-level tools like strings, dictionaries, and dates. The Foundation Framework defines several classes that provide the standard, object-oriented data structures found in typical high-level programming languages.

The Primitives module introduces Objective-C’s native types, and the rest of the above modules cover the fundamental Foundation classes. Below, you’ll find general information about interacting with these classes.

Creating Objects

There are two ways to instantiate objects in Objective-C. First, you have the standard alloc/init pattern introduced in the Classes module. For example, you can create a new NSNumber object with the following:

NSNumber *twentySeven = [[NSNumber alloc] initWithInt:27];

But, most of the Foundation Framework classes also provide corresponding factory methods, like so:

NSNumber *twentySeven = [NSNumber numberWithInt:27];

This allocates a new NSNumber object for you, autoreleases it, and returns it. Before ARC, this was a convenient way to create objects, but in modern programs, there is no practical difference between these two instantiation patterns.

In addition, numbers, strings, arrays, and dictionaries can be created as literals (e.g., @"Bill"). You’ll see all three notations throughout the Objective-C literature, as well as in this tutorial.

Comparing Objects

Comparing objects is one of the biggest pitfalls for Objective-C beginners. There are two distinct types of equality comparisons in Objective-C:

  • Pointer comparison uses the == operator to see if two pointers refer to the same memory address (i.e., the same object). It’s not possible for different objects to compare equal with this kind of comparison.
  • Value comparison uses methods like isEqualToNumber: to see if two objects represent the same value. It is possible for different objects to compare equal with this kind of comparison.

Generally, you’ll want to use the second option for more robust comparisons. The relevant methods are introduced alongside each data type (e.g., NSNumber’s Comparing Numbers section).

Mailing List

Sign up for my low-volume mailing list to find out when new content is released. Next up is a comprehensive Swift tutorial planned for late January.

Email Address:

You’ll only receive emails when new tutorials are released, and your contact information will never be shared with third parties. Click here to unsubscribe.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值