程式員過年回家沒有妹子怎麼辦?new一個貝

來源:互聯網
上載者:User

馬上就要回家過年了,身為偉大的程式員,過年怎麼能不帶個妹子回家呢,沒有妹子的怎麼辦?

沒關係,我們new一個貝


    NSMutableDictionary *me = [NSMutableDictionary dictionary];    // 從 GirlEntity類 建立一個妹子    GirlEntity *girl1 = [[[GirlEntity alloc] init] autorelease];    girl1.name = @"妹子1";    [me setObject:girl1 forKey:@"girlFriend"];


diaosi說從來沒有過妹子,不知道妹子類是什麼.

沒關係,我們自己造

    // 建立一個NSObject對象, 然後添加屬性,把他設定成妹子    NSObject *girl2 = [[[NSObject alloc] init] autorelease];    objc_setAssociatedObject(girl2, "name", @"妹子2", OBJC_ASSOCIATION_COPY);    [me setObject:girl2 forKey:@"girlFriend2"];

屌絲說這樣還不夠徹底.

沒關係,我們自己new一個類出來

// 動態建立一個妹子類,然後建立一個妹子    const char *className = "Girl3";    Class kclass = objc_getClass(className);    if (!kclass)    {        Class superClass = [NSObject class];        kclass = objc_allocateClassPair(superClass, className, 0);    }        NSUInteger size;    NSUInteger alignment;    NSGetSizeAndAlignment("*", &size, &alignment);    class_addIvar(kclass, "name", size, alignment, "*");        // 註冊到運行時環境    objc_registerClassPair(kclass);        id girl3 = [[[kclass alloc] init] autorelease];    object_setInstanceVariable(girl3, "name", "妹子3");        [me setObject:girl3 forKey:@"girlFriend3"];

如果想帶女神回去怎辦?

沒關係,我們可以挖牆角

// 挖牆角    GirlEntity *girl4 = [[[GirlEntity alloc] init] autorelease];    girl4.name = @"女神";        SEL original = @selector(talk);    SEL replacement = @selector(talk2);        Method a = class_getInstanceMethod([GirlEntity class], original);    Method b = class_getInstanceMethod([self class], replacement);    if (class_addMethod([GirlEntity class], original, method_getImplementation(b), method_getTypeEncoding(b)))    {        class_replaceMethod([GirlEntity class], replacement, method_getImplementation(a), method_getTypeEncoding(a));    }    else    {        method_exchangeImplementations(a, b);    }        [girl4 talk];-(void) talk2{    NSString *name = [self valueForKey:@"name"];    if ([name isEqualToString:@"女神"]) {        // do 你懂的        NSLog(@"%s", __FUNCTION__);    }}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.