NSLog 奇觀

提供了除了用define以外的其他各式各樣的寫法,
可以用function微調參數時比較方便

http://cocoaheads.byu.edu/wiki/different-nslog

A Quieter NSLog()


For some people, NSLog() writes out entirely too much stuff, including the program name, process ID, and current time down to the subsecond level. (Typical NSLog() output looks like: 2008-07-11 15:05:07.582 MyApp[1465] output.) Note that printf()can't be used as a drop-in replacement for NSLog() because it doesn't support the %@ format specifier for NSString objects. (If you're shaky on what format specifiers are, visit this article on Apple's site, and perhaps this brief tutorial to brush up on usage.)


QuietLog() is basically an Objective-C friendly version of printf() that you can use wherever you would normally use NSLog() (but would rather not see the extra spewage it produces). It accepts and outputs an NSString with printf-style format, including a variable number of parameters. For example, QuietLog(@"I have %d dogs and %d cats", dogs, cats); would print something like I have 5 dogs and 2 cats and a newline. Below are two possible implementations — the former uses a method only available in 10.5+ but the two versions are pretty much equivalent.
For 10.5 and later
void QuietLog (NSString *format, ...) {
    if (format == nil) {
        printf("nil\n");
        return;
    }
    // Get a reference to the arguments that follow the format parameter
    va_list argList;
    va_start(argList, format);
    // Perform format string argument substitution, reinstate %% escapes, then print
    NSString *s = [[NSString alloc] initWithFormat:format arguments:argList];
    printf("%s\n", [[s stringByReplacingOccurrencesOfString:@"%%" withString:@"%%%%"] UTF8String]);
    [s release];
    va_end(argList);
}

For 10.0 and later

void QuietLog (NSString *format, ...) {
    if (format == nil) {
        printf("nil\n");
        return;
    }
    // Get a reference to the arguments that follow the format parameter
    va_list argList;
    va_start(argList, format);
    // Perform format string argument substitution, reinstate %% escapes, then print
    NSMutableString *s = [[NSMutableString alloc] initWithFormat:format arguments:argList];
    [s replaceOccurrencesOfString:@"%%"
                       withString:@"%%%%"
                          options:0
                            range:NSMakeRange(0, [s length])];
    printf("%s\n", [s UTF8String]);
    [s release];
    va_end(argList);
}
The call to -initWithFormat:arguments: constructs a formatted string using an arbitrary number of parameters. The resulting string has %% replaced with %, which must be reversed before sending it to printf(). You can also replace printf("%s\n", ...) withfprintf(stderr"%s\n", ...) if desired.
If, like me, you prefer less information but would find it useful to also print the filename and line number where the call occurred, you could use something like this macro in concert with the above function:
#ifndef LocationLog
#define LocationLog(format,...) \
{ \
    NSString *file = [[NSString stringWithUTF8String:__FILE__] lastPathComponent]; \
    printf("%s:%d - ", [file UTF8String], __LINE__); \
    QuietLog((format),##__VA_ARGS__); \
}
#endif
For example, LocationLog(@"Hello World!"); would produce something like main.m:15 - Hello World!
In addition to  __FILE__ and  __LINE__, you can use __PRETTY_FUNCTION__ in a #define macro to get output like -[MyController awakeFromNib] when called from Objective-C methods. (From C code, it just prints the name of the calling function.)
This information was distilled from this post on Borkware.com, which could also be combined with the above approaches to customize the output exactly how you like it.

Where to Add This Code

For small programs, a handy place to put such code is in main.m, so everything in the application has access to it.
For larger compilation units, you'll want to declare the function (as shown below, using the extern keyword) in a header file that is included by any file that needs to use it (such as an Xcode pre-compiled header), then define it exactly as shown above in a .m file.
extern void QuietLog (NSString *format, ...);

Other Alternatives

The Log4Cocoa project is an Objective-C port of log4j, the popular Java based logging package.

Redirected NSLog()

Occasionally, you may want to redirect your NSLog() output to a file so that you can examine it more conveniently. NSLog() works by outputting messages to STDERR, so all you need to do is redirect the STDERR stream to a file, and you're good to go. The following code will redirect it to a file on your desktop:
int fd = creat ("/Users/dave/Desktop/my_log", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
close (STDERR_FILENO);
dup (fd);
close (fd);
NSLog(@"this will be written to my_log");
This will only affect NSLog() calls from your application.

NSLog() and Console.app

For yet another take on logging with NSLog, and specifically how it has changed in Leopard, visit the following URL:

#define log

You can put a custom log type in the prefix header file or any other global header.
It also allows you to disable logs completely by using #ifdefs
#define ExtendedLog( s, ... ) { \
NSLog( @"<%@:(%d)> %@ \t", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
                             __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) \
}

做armv7 跟i386 都可以用的library

簡單來說,就是把給simulator 還有給device用的.a 檔合併

相關資料有這些:

用了一個很少見的file 指令來做合併,不推薦

寫了一個模組來自動產生,但是有些微調的地方還是不夠

詳細作法,但這篇有點舊 還在ios SDK 3.x


所以....看了一堆對岸同胞的文章,作法簡敘如下:
其實只是這篇文章的中文翻譯



比如說我有一個專案叫做CPI,裡面有一堆檔案,按下build了以後會做出一個叫libCpi.a 的library
  1. 先用實機build一次,應該會build出實機的.a 檔
  2. 切成simulator 再build 一次
  3. add new Target ,選iOS 裡面的Other ,創一個Aggrete ,取一個甲意的專案名 
  4. 去 Build Settings裡面改PRODUCT_NAME ,改成原本build library 的那個專案名:CPI 
  5. 切到 Build Phases,按右下角 Add Build Phase 然後點Add Run Script 
  6. 中間會跑出來讓你填script 的欄位,填入
LIB_TARGET_NAME="CPI"

if [ "${ACTION}" = "clean" ]
then
echo "Cleaning Libraries..."
cd "${PROJECT_DIR}"
xcodebuild -target "$LIB_TARGET_NAME" -configuration ${CONFIGURATION} -sdk iphoneos clean
xcodebuild -target "$LIB_TARGET_NAME" -configuration ${CONFIGURATION} -sdk iphonesimulator clean
fi

if [ "${ACTION}" = "build" ]
then
echo "Building Libraries"
cd "${PROJECT_DIR}"
xcodebuild -target "$LIB_TARGET_NAME" -configuration ${CONFIGURATION} -sdk iphoneos
xcodebuild -target "$LIB_TARGET_NAME" -configuration ${CONFIGURATION} -sdk iphonesimulator

# Check that this is what your static libraries are called
ARM_FILES="${PROJECT_DIR}/build/${CONFIGURATION}-iphoneos/lib${LIB_TARGET_NAME}.a"
I386_FILES="${PROJECT_DIR}/build/${CONFIGURATION}-iphonesimulator/lib${LIB_TARGET_NAME}.a"

mkdir -p "${PROJECT_DIR}/build/Universal"

echo "Creating library..."
lipo -create "$ARM_FILES" "$I386_FILES" -o "${PROJECT_DIR}/build/Universal/lib${PRODUCT_NAME}.a"
fi


重點是第一行要設對,不然會build 錯,或者是Xcode 跑很久,久到睡個覺起來還沒跑好

重點結論,這個script 會在原資料夾下面/build/Universal ,做出一個universal 的library,
之後要用的時候就把.a 檔跟有用到的.h 檔加進專案就好了。


檢查方法如下:
去terminal 打 lipo -info library 的位置,正常的話會出現armv7 跟i386

webView safari debug tool

剛剛發現的,
safari 可以直接看iphone simulator 跟實機裡面webView的html,

用法如下

1.把code build 在simulator 裡面
2.打開safari ,先打開 開發人員選項
3.safari 上面那一列會出現 iPhone Simulator
    像圖中的youtubeid 就是我的project name ,
    因為webView 沒取head 所以是blank

我想webAPP 應該很需要這招,用Ti開發的app 應該也可以用到~


自動產生Icon files

根據前幾天的ios icon清單,用機器人寫了一個小工具
只要給他一張144x144以上的圖檔,
就會產生出所有不同大小的icon,
我一般都是把 1024x1024上架時那張當做source丟進去

iphone icon產生器
ipad icon產生器
而且把它改icon後供在桌面上



推播加聲音

{
  "aps": {
    "badge": 10,
    "alert": "Hello world!",
    "sound": "yell.mp3"
  }
}

就會直接播app內的yell.mp3這個檔案

{
  "aps": {
    "badge": 10,
    "alert": "Hello world!",
    "sound": "yell.mp3"
  },
  "customID": 1
}
還可以做到customID這種事


reference:
Local and Push Notification Programming Guide

ios icon清單

App-Related Resources
之前只需要用icon.png  還有icon@2x.png
上個月同事上架遇到奇怪的問題,
才發現有這一串東西要設定,

最神奇的是Icon 的I 要大寫喔,為什麼要在這種小地方做手腳呢

Icon
Idiom
Size
Usage
App icon (required)
iPhone
57 x 57 pixels
114 x 114 pixels (@2x)
This is the main icon for apps running on iPhone and iPod touch.
App icon (required)
iPad
72 x 72 pixels
144 x 144 pixels (@2x)
This is the main icon for apps running on iPad.
Small icon for Spotlight search results and Settings (recommended)
iPhone
29 x 29 pixels
58 x 58 pixels (@2x)
This is the icon displayed in conjunction with search results on iPhone and iPod touch. This icon is also used by the Settings app on all devices.
Small icon for Spotlight search results and Settings (recommended)
iPad
50 x 50 pixels
100 x 100 pixels (@2x)
This is the icon displayed in conjunction with search results on iPad.

ios simulator 修改設定

他在這邊
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app/Contents/Resources/

改frame.png 就可以換 框框

讀書心得


闖世代:就用熱血打敗全球倦業潮吧!
是挑戰,也是機會的新時代《闖世代:就用熱血打敗全球倦業潮吧!》推薦序

【他們拒當青貧族 闖出大成就】闖世代:用熱血打敗全球倦業潮

       剛剛在書店看到這本書,整篇都在熱血,簡直就是在看文字版的魁男塾,只差沒有說出「男兒,當立死志」這句話而已。就是那種站著把它看完,看完手捧著書在發抖的書

      前幾天看到的學徒模式裡面也是談到同樣的觀念,熱情、熱血、passion。只要找到自己有興趣的領域,拼死命的鑽研,一定會有所斬獲。我很慶幸我找到一個跟興趣一樣的工作,上班下班幾乎都是在做一樣的事。這讓我也常常在想:還能怎麼做?我一直覺得我們擁有更多的可能性,我們值得站上更大的舞台,只是還沒做出值得我們驕傲的作品而已,不停嘗試、不停衝撞,只要我們的熱情還不消散,一定會完成的。




丘成桐談空間的內在形狀

       其實這本書有一大半數學是看不懂也沒聽過的,只是想了解丘大師平常的為人還有他做研究的態度而已。其實看了前面兩章,我就知道我已經看到我想看的了,我發現很有趣的一點是:大師之所以是大師,除了很會利用時間以外、還會願意去推動一些事情,這跟我之前在學校認識的教授們,簡直就是一模一樣,只是丘大師的學生更多、領域更廣而已。
       這本書定位是科普書,所以前面花了一些篇幅在講幾何、向量、微積分之類的觀念,其實阿...很明顯感覺得出來老師的熱情,但是我覺得還是講得不夠科普,或許剛修過微積分的大一小朋友還看得下去,但是年齡成更低的應該就直接束之高閣了吧,我覺得我有必要哪天再拿出來翻翻。