《Objective-C 基础教程》笔记

1. Hello

2. Extensions to C

#import

NSLog

%@

@"string"

BOOL YES NO

3. OOP

self
   
/* class.h BEGIN */
@interface Class:NSObject
{
    int _member;
}   

- (int)function:(int)parameter;

- (void)many_parameters_function:(int)parameter1 some_information:(NString *)parameter2;

- (void)no_parameter_function;
@end //Class
/* class.h END */
/* class.m BEGIN */
@implementation Class
- (int)function:(int)p 
{
}//function
- (void)many_parameters_function:(int)p1 some_information:(NString *)p2
{
}//many_parameters_function
- (void)no_parameter_function
{
} //noparameter_function
@end //Class
/* class.m END */
Objective-C does not support multiple inheritance
 /* Children.h BEGIN */
@interface Children : Parent

@end //Children
/* Children.h END */

super

isa()

overridden

5. Composition

description

6. Organization

@class sets up a forward reference

7. More About Xcode

defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions

'{"ORGANIZATIONNAME" = "zuohaitao";}'

command+shift+E

File->Make Snapshot

File->Snapshots

command+D

Help->Show Research Assistant.

8. Foundation Kit

  NSRange
    typedef struct _NSRange NSRange;
    struct _NSRange
    {
        NSUInteger location;
        NSUInteger length;
    };
  NSPoint
    typedef struct _NSPoint NSPoint;
    struct _NSPoint
    {
        CGFloat x;
        CGFloat y;
    };
  NSSize
    typedef struct _NSSize NSSize;
    struct _NSSize
    {
        CGFloat width;
        CGFloat height;
    };
  NSRect
    typedef struct _NSRect NSRect;
    struct _NSRect
    {
        NSPoint origin;
        NSSize size;
    };
  NSString
    + (id)stringWithFormat:(NSString *)format,...
    - (unsigned int)length
    - (BOOL)isEqualToString:(NSString *)aString
    - (NSComparisonResult)compare:(NSString *) string;
    - (NSComparisonResult)compare:(NSString *) string 
                          options:(unsigned) mask;
    - (BOOL)hasPrefix:(NSString *)aString;
    - (BOOL)hasSuffix:(NSString *)aString;
    - (NSRange)rangeOfString:(NSString *) aString;
    - (NSArray *)componentsSeparatedByString:(NSString *)separator
    - (NSString *)componentsJoinedByString:(NSString *)separator
    - (NSString *)stringByExpandingTildeInPath
  NSMutableString
    + (id)stringWithCapacity:(unsigned)capacity;
    - (void)appendString:(NSString *)aString;
    - (void)appendFormat:(NSString *)format, ...;
    - (void)deleteCharactersInRange:(NSRange)range;
  NSArray
    + (id)arrayWithObjects:(id)firstObj,...;
    - (unsigned)count;
    - (id)objectAtIndex:(unsigned int) index;
  NSMutableArray
    + (id)arrayWithCapacity:(unsigned) numItems;
    - (void)addObject:(id)anObject;
    - (void)removeObjectAtIndex:(unsigned)index;
    - (NSEnumerator *)objectEnumerator;
    - (id)nextObject;
        /* enumeration */
        NSEnumerator *enumerator;
        enumerator = [array objectEnumerator];
        id thingie;
        while(thingie = [enumerator nextObject]) {
            NSLog(@"I found %@", thingie);
        }
        /* Fast Enumeration */
        for(NSString *string in array) {
            NSLog(@"I found %@", string);
        }
  NSDictionary
    + (id)dictionaryWithObjectsAndKeys:(id)firstObject, (id)firstKey, ...;
    - (id)objectForKey:(id)aKey;
  NSMutableDictionary
    + (id)dictionaryWithCapacity:(unsigned int)numItems;
    - (void)setObject:(id)anObject forKey:(id)aKey;
    - (void)removeObjectForKey:(id)aKey;

because in Cocoa may classes are implemented as class clusters,

don't create subclass to extend, use categories.

  NSNumber
    + (NSNumber *)numberWithChar:(char)value;
    + (NSNumber *)numberWithInt:(int)value;
    + (NSNumber *)numberWithFloat:(float)value;
    + (NSNumber *)numberWithBool:(BOOL)value;
    - (char)charValue;
    - (int)intValue;
    - (float)floatValue;
    - (BOOL)boolValue;
    - (NSString *)stringValue;
  NSValue
    + (NSValue *)valueWithBytes:(const void *)value
                       objCType:(const char *)type;
    + (NSValue *)valueWithPoint:(NSPoint)point;
    + (NSValue *)valueWithSize:(NSSize)size;
    + (NSValue *)valueWithRect:(NSRect)rect;
    - (NSPoint)pointValue;
    - (NSSize)sizeValue;
    - (NSRect)rectValue;
  NSNull
    + (NSNull *) null;
  NSFileManager
    + (NSFileManager *)defaultManager
    - (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path

9.0 Memory Management

  • Garbage Collection(GC)

If you know that your programs will only be run on Leopard or later, you can take advantage of Objective-C 2.0's garbage collection

  • Reference Counting(RC)

Automatic Reference Counting(ARC)

ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5.

Weak references are not supported in OS X v10.6 and iOS 4.

  • Manual Reference Counting(MRC)

    - (id)retain;
    -(oneway void)release;
    

oneway is used with the distributed objects API, which allows use of objective-c objects between different threads or applications. It tells the system that it should not block the calling thread until the method returns. Without it, the caller will block, even though the method's return type is void. Obviously, it is never used with anything other than void, as doing so would mean the method returns something, but the caller doesn't get it.

    - (unsigned)retainCount;
    - (id)autorelease;

The Rules of Cocoa Memory Management

   +----------------+-------------------------+--------------------------------------------+
   |Obtained Via... |Transient                |Hang On                                     |
   +----------------+-------------------------+--------------------------------------------+
   |alloc/new/copy  |Release when done        | Release in dealloc                         |
   +----------------+-------------------------+--------------------------------------------+
   |Any other way   |Don't need to do anything| Retain when acquired, release in dealloc   |
   +----------------+-------------------------+--------------------------------------------+
    /* Keeping The Pool Clean */
    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init];
    int i;
    for (i = 0; i < 1000000; i++) {
        id object = [someArray objectAtIndex: i];
        NSString *desc = [object descrption];
        // and do something with the description
        if (i % 1000 == 0) {
        [pool release];
        pool = [[NSAutoreleasePool alloc] init];
        }
    }
    [pool release]
    /* Keeping The Pool Clean */

10. Object Initialization

11. Properties

Objective-C 2.0 features can only be used on Mac OS X 10.5 (Leopard) or later

@property

assign retain copy

readonly readwrite

nonatomic

@synthesize

12.Categories

        @interface ClassName(CategoryName)

        @end //interface ClassName(CategoryName)
        @implementation ClassName(CategoryName)

        @end //implementation ClassName(CategoryName)
  • Bad Category

    1. You can not add variables to class.

    2. When names collide, the category wins.

  • Purpose

    1. split class implementation into multiple files or multiple frameworks

    2. creating forward references for private methods

    3. adding informal protocols to an object

  • Delegate

delegate is an object asked by another object to do some of its work.

e.g. the AppKit class NSApplication asks its delegate if it should open an Untitled window when the application launches.

@selector(func:)

[obj respondsToSelector:@selector(func:)]

13. Protocols

    @protocol FormalProtocolA

    - (void)functionA;

    @end //protocol FormalProtocolA
    @protocol FormalProtocolB
    - (void)functionB;
    @end //protocol FormalProtocolB
    @interface Obj:NSObject
    @end //interface Obj
    @implementation Obj
    - (void)functionA
    {
    }
    - (void)functionB
    {
    }
    @end //interface Obj
  • A shallow copy

    you don't duplicate the referred objects;

    you new copy simply points at the referred objects that already exist.

  • A deep copy

    makes duplicates of all the referred objects.

    - (id)copyWithZone:(NSZone *)zone
    {
        return [[[self class] allocWithZone: zone]init];
    }
  • Objective-C 2.0

  • @optional

  • @required

标签: objc
日期: 2013-04-15 17:30:06, 11 years and 276 days ago

删除Windows write live 2011上最后一个帐号

There is a terrible user experience in Windows Live Writer 2011. Windows Live Writer 2011有个坑爹的设计,

You can not delete the last blog account, because of the delete button is disabled. 你不能通过界面删除最后一个博客帐号。

But, you can edit regedit to delete the last account 可以通过编辑注册表去删除帐号

Find "HKEY_CURRENT_USER\Software\Microsoft\Windows Live\Writer\WebLogs" the random GUID that denotes your account. 找到"HKEY_CURRENT_USER\Software\Microsoft\Windows Live\Writer\WebLogs"删除名为随机的GUID的子键

Deleting Your Last And Only Blog From Windows Live Writer

标签: other
日期: 2013-04-09 17:30:06, 11 years and 282 days ago
  • Register 注册

  • Create repository 创建库
    新建库 可以在任何有权限的账户上创建库,无论是个人或组织帐号

    1. 在任意页面的右上角的用户条,点击“Create a New Repo"按钮
    2. 选择创建库的帐号
    3. 输入库名字,选择库类型public公有或private私有;然后点击“Create repository”
  • Delete repository 删除库 删除库

    1. 切换至要删除的库
    2. 选择库操作条上的“Settings”
    3. 点击“Delete this repository”在Danger Zone 区域
    4. 阅读警告,输入库名
    5. 点击“I understand the consequences, delete this repository”
  • Establish a secure connectio SSH Keys 如果决定不使用推荐的HTTPS方法,也能用SSH keys去建立一个安全的终端到github的链接, 下面的步骤将引导你产生一个SSH key然后加入公钥到github帐号

    1. 检查SSH keys 首先,我们需要检查一个存在的ssh keys在你的计算机。 cd ~/.ssh 如果没有文件或路路,那么进行第二步,否则密钥对已存在那么跳到第三步。
    2. 产生新的SSH key 产生新的SSH key ssh-keygen -t rsa -C "your_email@example.com"
    3. 加入SSH key到GitHub 拷贝公钥到剪切版 ~/.ssh/id_rsa.pub
      1. 进入账户设置“Account Settings”
      2. 点击侧栏的“SSH Keys”
      3. 点击“Add SSH key”
      4. 黏贴公钥到“Key”区域
      5. 点击“Add key”
      6. 确认密码后,添加成功
    4. 测试 ssh -T git@github.com
标签: git
日期: 2013-04-06 17:30:06, 11 years and 285 days ago