NSString *varName
- (void)doSomethingWithString:(NSString *)theString {
...}
- (void)doSomethingWith:(GTMFoo *)theFoo
rect:(NSRect)theRect
interval:(float)theInterval {
...}
- (void)short:(GTMFoo *)theFoo
longKeyword:(NSRect)theRect
evenLongerKeyword:(float)theInterval {
...}
- (BOOL)isBold {
return [self fontTraits] & NSFontBoldTrait;
} // 禁止
- (BOOL)isValid {
return [self stringValue];
} // 禁止
- (BOOL)isBold {
return ([self fontTraits] & NSFontBoldTrait) ? YES : NO;
} // 对头
- (BOOL)isValid {
return [self stringValue] != nil;
} // 对头
- (BOOL)isEnabled {
return [self isValid] && [self isBold];
} // 对头
+new
方法,使用alloc
和init
方法。@interface RootViewController : UITableViewController<
UITableViewDelegate,
UITableViewDataSource,
UITextFieldDelegate,
UITextViewDelegate >
struct Books
{
NSString *title;
NSString *author;
NSString *subject;
int book_id;
} book;
//此声明声明了拥有3个成员的结构体,分别为整型的a,字符型的b和双精度的c
//同时又声明了结构体变量s1
//这个结构体并没有标明其标签
struct
{
int a;
char b;
double c;
} s1;
//此声明声明了拥有3个成员的结构体,分别为整型的a,字符型的b和双精度的c
//结构体的标签被命名为SIMPLE,没有声明变量
struct SIMPLE
{
int a;
char b;
double c;
};
//用SIMPLE标签的结构体,另外声明了变量t1、t2、t3
struct SIMPLE t1, t2[20], *t3;
//也可以用typedef创建新类型
typedef struct
{
int a;
char b;
double c;
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[20], *u3;
typedef enum
{
//以下是枚举成员
TestA = 0,
TestB,
TestC,
TestD
}Test;//枚举名称
typedef NS_ENUM(NSInteger, Test1)
{
//以下是枚举成员
Test1A = 0,
Test1B = 1,
Test1C = 2,
Test1D = 3
}; // suggest
typedef NS_ENUM(NSInteger, Test)
{
TestA = 1, //1 1 1
TestB = 1 << 1, //2 2 10 转换成 10进制 2
TestC = 1 << 2, //4 3 100 转换成 10进制 4
TestD = 1 << 3, //8 4 1000 转换成 10进制 8
TestE = 1 << 4 //16 5 10000 转换成 10进制 16
}; // 位运算
/*
什么时候要用到这种方式呢?
那就是一个枚举变量可能要代表多个枚举值的时候. 其实给一个枚举变量赋予多个枚举值的时候,原理只是把各个枚举值加起来罢了.
当加起来以后,就获取了一个新的值,那么为了保证这个值的唯一性,这个时候就体现了位运算的重要作用.
位运算可以确保枚举值组合的唯一性.
因为位运算的计算方式是将二进制转换成十进制,也就是说,枚举值里面存取的是 计算后的十进制值.
打个比方:
通过上面的位运算方式设定好枚举以后,打印出来的枚举值分别是: 1 2 4 8 16
这5个数字,无论你如何组合在一起,也不会产生两个同样的数字.
手工的去创建位运算枚举,还有稍微有点花时间的,好在Apple已经为我们准备的uint.所以,用下面这种方式来初始化一个位运算枚举吧:
*/
typedef NS_ENUM(uint, Test)
{
TestA,
TestB,
TestC,
TestD,
TestE
};
/*
在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。
NS_OPTIONS一般用来定义位移相关操作的枚举值,我们可以参考UIKit.Framework的头文件,可以看到大量的枚举定义。
*/
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,//默认从0开始
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
// NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。
创建一个import所有宏相关的文件Macros.h
在xcode项目的pch文件中,导入Macros.h文件
Macros.h
#import "UtilsMacros.h"
#import "APIStringMacros.h"
#import "DimensMacros.h"
#import "NotificationMacros.h"
#import "SharePlatformMacros.h"
#import "StringMacros.h"
#import "UserBehaviorMacros.h"
#import "PathMacros.h"
XcodeProjectName-Prefix.pch
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Macros.h"
#endif
NSDictionary和NSMutableDictionary常用方法总结
这是一篇关于沙盒的基础知识教程。简述沙盒的作用,对Documents、Library、tmp之间的区别做了介绍。通过两种方法打开沙盒,查看其中的内容。
沙盒里的文件夹包括Documents、Library、tmp。文章介绍了如何获取Documents、Library、Caches、tmp的路径。
通过图文的方式详细讲解如何查看真机沙盒。
NSFileManager是一个单列类,也是一个文件管理器。可以通过NSFileManager创建文件夹、创建文件、写文件、读文件内容等等基本功能。
操作系统iOS 中应用程序使用的计算机内存不是统一分配空间,运行代码使用的空间在三个不同的内存区域,分成三个段:“text segment “,“stack segment ”,“heap segment ”。