Objective-C literal syntax

1.NSNumber

old:NSNumber *oldNumber = [NSNumber numberWithInt:1];

new:NSNumber *newNumber = @1;

同理:

NSNumber *intNumber =
@1;

NSNumber *floatNumber =
@1.0f;

NSNumber *doubleNumber =
@0.001;

NSNumber *boolNumber =
@YES;

NSNumber *charNumber =
@‘a‘;

2.NSArray

old:NSArray *oldArray = [NSArray
arrayWithObjects:@"David",@"Tom",@"Jack",nil];

new:NSArray *newArray = @[@"David",@"Tom",@"Jack"];

old:NSString *Tom = [oldArray objectAtIndex:1];

new:NSString *Tom = newArray[1];

区别:

NSArray *oldArray = [NSArray
arrayWithObjects:@"David",nil,@"Tom",@"Jack",nil];//创建成功,数组只含一个元素

NSArray *newArray = @[@"David",nil,@"Tom",@"Jack"];//创建失败,报错

3.NSDictionary

old:NSDictionary *oldDic = [NSDictionary
dictionaryWithObjectsAndKeys:@"Eric",@"firstName",@"Yin",@"lastName",[NSNumber
numberWithInt:16],@"age"];

new:NSDictionary *newDic =
@{@"firstName":@"Eric",@"lastName":@"Yin",@"age":@26};

old:NSString *firstName = [oldDic objectForKey:@"firstName"];

new:NSString *firstName = newDic[@"firstName"];

4.NSMutableArray,NSMutableDictionary(setting)

old:

[mutableArray
replaceObjectAtIndex:1
withObject:@"Jason"];

[mutableDic setObject:@"Tom"
forKey:@"firstName"];

new:

mutableArray[1] = @"Jason";

mutableDic[@"firstName"] = @"Tom";

Objective-C literal syntax,布布扣,bubuko.com

时间: 2024-12-28 21:10:30

Objective-C literal syntax的相关文章

Effective Objective-C 2.0 笔记三(Literal Syntax简写语法)

当使用Objective-C的时候,你总会遇到Foundation 框架中的一些类,这些类包括NSString,NSNumber,NSArray和NSDictionary,这些数据结构都是自解释的. Objective-C以简明详细的语法而著名,自从oc1.0有一个简单的方式定义一个NSString变量,我们可以这样声明一个字符串变量 NSString *[email protected]"Hello Lves"; 没有这种语法之前,我们创建一个字符串变量需要先alloc然后init.

自定义类实现基于数组/字典Literal Syntax设置和获取数据

.h文件 #import <Foundation/Foundation.h> @interface TestKVC : NSObject {          NSMutableDictionary *mDictionary;     NSMutableArray *mArray; } - (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey; - (id)objectForKeyedSubscrip

Objective - c Foundation 框架详解2

Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such as NSArray and NSDictionary whose instances exist just to hold onto other objects. cocoa 提供了一系列的集合类,例如,NSarray,NSdictionary.它们存在的目的就是为了保持其他对象. 1.1.1N

C++ Objective C Java C 详细比较和区别

分享一下我老师大神的人工智能教程吧.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net primitive types | arithmetic and logic | strings | regexes | dates and time | arrays | dictionaries | functions execution control | files | directories | processes and

JavaScript Patterns 3.6 Regular Expression Literal

1. Using the new RegExp() constructor // constructor var re = new RegExp("\\\\", "gm"); 2. Using the regular expression literal // regular expression literal var re = /\\/gm;  when using the RegExp()constructor, you also need to escape

ES6 Syntax and Feature Overview(待续)

View on GitHub Note: A commonly accepted practice is to use const except in cases of loops and reassignment. However, in this resource I'll be using let in place of var for all ES6 examples. Variable: x Object: obj Array: arr Function: func Parameter

A Swifr Tour

Tradition suggests that the first program in a new language should print the words "Hello ,world!" on the screen. In Swift , this can be done in a single line :  print("Hello world") If you gave written code in C otr Objective - C , th

[iOS笔记]《编写高质量iOS与OS X代码的52个有效方法》:1.熟悉Objective-C

简介: 最近公司项目收尾,可以有时间看看书了.<编写高质量iOS与OS X代码的52个有效方法>这本书讲解了很多iOS开发的技巧和规范,大力推荐! 下面是自己看书时整理的笔记,照惯例先上目录: 目录: 第一章:熟悉Objective-C 第二章:Object.Message.Runtime 第三章:接口与API设计 第四章:Protocol与Category 第五章:内存管理 第六章:Block与GCD 第七章:系统框架 第一章    熟悉Objective-C 第1条:了解Objective

javascript闭包(Effective JavaScript读书笔记)

Effective JavaScript:编写高质量JavaScript代码的68个有效方法: Item 11:   Get Comfortable with Closures Closures may be an unfamiliar concept to programmers coming from languages that don’t support them. And they may seem intimidating at first. But rest assured tha