NSURL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
NSString *urlString = @"http://www.books.com:3321/lib/query;steve?key=job&sort=asc#chapter2";
NSURL *url = [NSURL URLWithString:urlString];

NSLog(@"url.absoluteString %@",url.absoluteString);
NSLog(@"url.absoluteURL %@",url.absoluteURL);
NSLog(@"url.baseURL %@",url.baseURL);

NSLog(@"url.scheme %@",url.scheme);
NSLog(@"url.host %@",url.host);
NSLog(@"url.port %@",url.port);
NSLog(@"url.query %@",url.query);
NSLog(@"url.fragment %@",url.fragment);
NSLog(@"url.parameterString %@",url.parameterString);
NSLog(@"url.pathExtension %@",url.pathExtension);
NSLog(@"url.path %@",url.path);
NSLog(@"url.pathComponents %@",url.pathComponents);
NSLog(@"url.lastPathComponent %@",url.lastPathComponent);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
url.absoluteString http://www.books.com:3321/lib/query;steve?key=job&sort=asc#chapter2
url.absoluteURL http://www.books.com:3321/lib/query;steve?key=job&sort=asc#chapter2
url.baseURL (null)
url.scheme http
url.host www.books.com
url.port 3321
url.query key=job&sort=asc
url.fragment chapter2
url.parameterString steve
url.pathExtension
url.path /lib/query
url.pathComponents (
"/",
lib,
query
)
url.lastPathComponent query

Size Class

iPhone4S,iPhone5/5s,iPhone6
竖屏:(w:Compact h:Regular)
横屏:(w:Compact h:Compact)
iPhone6 Plus
竖屏:(w:Compact h:Regular)
横屏:(w:Regular h:Compact)
iPad
竖屏:(w:Regular h:Regular)
横屏:(w:Regular h:Regular)

NSObject load 和 initialize 的调用时间和次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@interface Animal : NSObject  @end
@interface Cat : Animal @end
@interface Dog : Animal @end

@implementation Animal
+ (void)load {
NSLog(@"in Animal load %@",NSStringFromClass([self class]));
}
+(void) initialize{
NSLog(@"in Animal initialize with %@",NSStringFromClass([self class]));
}
@end

@implementation Cat @end
@implementation Dog @end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@">1");
Cat *cat0 = [[Cat alloc] init];
NSLog(@">2");
Dog *dog0 = [[Dog alloc] init];
NSLog(@">3");
Cat *cat1 = [[Cat alloc] init];
NSLog(@">4");
Dog *dog1 = [[Dog alloc] init];
return YES;
}
@end

####console output

Animal initialize with Animal
1
2
3
4
5
6
7
in Animal load Animal
>1
in Animal initialize with Cat
>2
in Animal initialize with Dog
>3
>4

KVO 几个options

1
2
3
4
5
6
7
8
NSKeyValueObservingOptions.New   新值
NSKeyValueObservingOptions.Old    旧值
NSKeyValueObservingOptions.Initial 注册监听的时候会立即发送
NSKeyValueObservingOptions.Prior  更改之前会额外发送一次
 
只有 NSKeyValueObservingOptions.New
和 NSKeyValueObservingOptions.Old   
会存在于 observeValueForKeyPath 方法的change dictionary中

swift 通过 string 创建 object

1
2
3
4
5
6
7
8
9
10
11
class IndexViewController: UIViewController{}
 
let className = "IndexViewController"
 
let bundlePath = NSBundle.mainBundle().bundlePath
let bundleFullName = bundlePath.componentsSeparatedByString("/").last
let bundleName = bundleFullName?.componentsSeparatedByString(".").first
let clazz = NSClassFromString(bundleName! + "." + className)! as! UIViewController.Type
 
let object = clazz.init()
// let object1 = Index() 普通方法

cocoa 查看无线是否打开 可链接的wifi列表

1
2
3
4
5
6
7
8
9
10
11
CWInterface *wifi = [CWWiFiClient sharedWiFiClient].interface;
 
NSLog(@"interfaceName: %@", wifi.interfaceName);
NSLog(@"ssid: %@", wifi.ssid);
NSLog(@"powerOn: %@", @(wifi.powerOn));
 
NSSet<CWNetwork *> *result = [wifi scanForNetworksWithName:nil error:nil];
NSLog(@"可连接的wifi列表");
for (CWNetwork *each in result) {
    NSLog(@"--->%@",each.ssid);
}

cocoa播放音频 系统声效

1
2
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"alert" ofType:@"wav"];
_sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];

播放系统声效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int soundIndex = 0;
- (void) tryLoopPlaySound{
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/System/Library/Sounds"
error:nil];
NSUInteger arrayCount = [directoryContents count];
if (soundIndex < arrayCount-1) {
soundIndex++;
}
else {
soundIndex = 0;
}
[self playSystemSound:soundIndex];
}

-(void) playSystemSound:(int) index{
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/System/Library/Sounds"
error:nil];
NSUInteger arrayCount = [directoryContents count];
if(index >= arrayCount) {
return;
}
NSString *soundName = [directoryContents objectAtIndex:
index];
soundName = [soundName stringByDeletingPathExtension];
NSSound *systemSound = [NSSound soundNamed:soundName];
NSLog(@"soundName %@ %d",soundName, index);
[systemSound play];
}