2015年7月27日 星期一

為何int的負數會比正數多1

因為C語言中做了一個規定
當最高位的bit為1而其他都為0的時候,這個1既表示負號又表示它的數值。
以32bit為例,有號數的int範圍為
正: 2147483647 ~ 0  負: -1 ~ -2147483648

如此定義有二個好處,
一是計算方便,當你用2的補數往上做加法時,可以直接依二進制計算,不用另外調整正負號。
二是避免重覆定義0, 因為+0跟-0是同一個數,不需要分正負。

2015年7月24日 星期五

使用之前的cookie

//儲存
let urlEnconded = sessionId!.stringByAddingPercentEncodingWithAllowedCharacters(
                NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]").invertedSet)!
            let properties = [NSHTTPCookieDomain: WebURL.URL_MIGME_SERVER,
                NSHTTPCookiePath: "\\",
                NSHTTPCookieName: "eid",
                NSHTTPCookieValue: urlEnconded + MigboClient.cookiePrefix]
            if let cookie = NSHTTPCookie(properties: properties) {
                NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie)
                System.setShareProperties(DefaultConfig.SHARE_SYS_COOKIES, value: properties)

            }

//使用
var url = NSURL(string: "yourUrl")
mutableRequest = NSMutableURLRequest(URL:url)
var cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as! [NSHTTPCookie]
var headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies)
mutableRequest!.allHTTPHeaderFields = headers

self._webview.loadRequest(mutableRequest ?? NSURLRequest(URL:url))

2015年7月22日 星期三

String pool 概念

String Pool是為了解決太多內容同樣的String物件,造成記憶體空間浪費的問題,
實作上建立了一個hashMap,把字串內容轉換為key值來查找String物件實體,如果有則拿來用,如果沒有才new一個新的。

reflection概念


Reflection, 用在 Java 身上指的 是我們可以於執行期載入、探知、使用編譯期間完全未知的 classes。

Java 反射機制 - 侯捷

iOS也可以實現reflection的機制,關鍵字如下:
objc_getClass
objc_property_t
class_copyPropertyList
class_replaceMethod

2015年7月21日 星期二

如何抓到目前的thread標籤

//Objective-C
NSLog(@"%s", dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL));

//swift
println(String(UTF8String: dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL))!)

2015年7月20日 星期一

navigationController連續push的crash問題

//實作UINavigationControllerDelegate
func navigationController(navigationController: UINavigationController,
        didShowViewController viewController: UIViewController,
        animated: Bool)
    {
        //TODO:擋住新的push或是在這裡放closure來push新的viewController
    }

2015年7月16日 星期四

Custom Preprocess Flag 設置自定義的預處理的方式

OTHER_CFLAGS (Other C Flags)
// 在Target>Build Setting>Custom Compiler Flags>Other C Flags
// 或Target>Build Setting>Swift Compiler>Custom Flags>Other Swift Flags
// 前面要加-D。舉例來說-DRELEASE -DDEVELOPMENT

GCC_PREPROCESSOR_DEFINITIONS (Preprocessor Macros)
// 在Target>Build Setting> Preprocessing > Preprocessor Macros

INFOPLIST_PREPROCESSOR_DEFINITIONS (Info.plist Preprocessor Definitions)
// 在Target>Build Setting> Packaging > Info.plist Preprocessor Definitions

2015年7月9日 星期四

Xcode的Debug小技巧

加入之後,可以看到解釋清楚的crash原因。

ASTableView的update方式

self._tableView.beginUpdates()
                self._tableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)
                self._tableView.reloadSections(indexSet, withRowAnimation: UITableViewRowAnimation.None)
                self._tableView.endUpdates()

a social app needs to have precautions mechanism.

14.3 Details Your app enables the display of user-generated content but does not have the required precautions in place. Next Steps It
July 9, 2015 at 8:43 AM
From Apple
14.3 - Apps that display user generated content must include a method for filtering objectionable material, a mechanism for users to flag offensive content, and the ability to block abusive users from the service
14.3 Details

Your app enables the display of user-generated content but does not have the required precautions in place.

Next Steps

It is necessary that you put all of the following precautions in place:

- Require that users agree to terms (EULA) and these terms must make it clear that there is no tolerance for objectionable content
- Use moderators to flag and remove inappropriate content and offensive users
- Users need a mechanism to flag objectionable content and report users generating this content
- Developer must act on objectionable content reports within 24 hours by removing the content and ejecting the user who provided the offending content
- Developer needs a method for ejecting users who violate the terms of the EULA

Reply Use the field below to ask questions or provide additional information to the App Review team. Learn More Submit an appeal to the App Review Board.This field is required. 4000
Attach File
Send

Therefore, we have three task need to be finished:1. Term agree, 2. block user, 3. report abuse content

2015年7月6日 星期一

在yourUIView.swift中與xib做聯結

required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.view = NSBundle.mainBundle().loadNibNamed("ChatPanelView", owner: self, options: nil)[0] as! UIView
        self.view.frame = CGRectMake(0, 0, DefaultConfig.SCREEN_WIDTH, 87)
        self.addSubview(self.view)
        self.setNeedsUpdateConstraints()
        self.inputTextField.text = Language.get("TYPE_A_MESSAGE", comment: "Type a message")
        self.inputTextField.scrollEnabled = false

    }