2009年12月21日星期一
學習compare NSSTring Objects
第一個想到的是"==",但是這個不正確,應該是 isEqualToString
NSString *str1 = @"Homebrew";
NSString *str2 = @"Homebrew";
if(str1 == str2)
{
NSLog(@"str1 equals str2");
}else{
NSLog(@"Str1 does not equal str2");
}
NSLog(@"str1 address in memory : %p", str1);
NSLog(@"str2 address in memory : %p", str2);
char * cStr = "Homebrew";
NSString *str3 = [NSString stringWithUTF8String:cStr];
NSString *str4 = @"Homebrew";
if(str3 == str4)
{
NSLog(@"str3 equals to str4");
}
else{
NSLog(@"str3 does not equals to str4");
}
NSLog(@"str3 address in memory is %p", str3);
NSLog(@"str4 address in memory is %p", str4);
if([str3 isEqualToString: str4])
{
NSLog(@"str3 equals str4");
}else{
NSLog(@"str3 does not equal str4");
}
結果:
[Session started at 2009-12-22 09:58:59 +0800.]
2009-12-22 09:58:59.699 Movie_Player2[34747:10b] str1 equals str2
2009-12-22 09:58:59.710 Movie_Player2[34747:10b] str1 address in memory : 0x2030
2009-12-22 09:58:59.710 Movie_Player2[34747:10b] str2 address in memory : 0x2030
2009-12-22 09:58:59.711 Movie_Player2[34747:10b] str3 does not equals to str4
2009-12-22 09:58:59.711 Movie_Player2[34747:10b] str3 address in memory is 0x105bc0
2009-12-22 09:58:59.712 Movie_Player2[34747:10b] str4 address in memory is 0x2030
2009-12-22 09:58:59.713 Movie_Player2[34747:10b] str3 equals str4
The Debugger has exited with status 0.
參考解釋是:
The reason this works is that the complier can manage strings internally when you define them using the shortcut method(@"stringhere") and will store only one reference internally to duplicates.
You can verify that the strings refer to the same content by looking at the locations in memory where the variables are stored.
The Correct way to compare Strings
the right way to go about this is use the isEqualToString: method in the NSString class
ref:http://iphonedevelopertips.com/cocoa/compare-nsstrings-objects.html
2009年11月18日星期三
Objective-C 學習筆記
最近正在學習iPhone 開發,記錄一下
常用 Key words
. Messaging
. Import Syntax
. Declaring Method Headers
. Inheritance Synax
. property and synthesize keywords
. interface and implementation Declarations
. protocols
. self keywords.
. Id keyword
Messaging: --可以理解為method (c/c++/java)
Example:
[object message];
similar to :
object.method();
object->method();
Messaging with parameters
Example: 含參數的例子
[object message: param1 withParameter:param2];
Similar to:
object.method(param1, param2);
object->method(param1,param2);
Nestd Messages :
example:嵌套調用
[object secondMessage:[object message]]
Similar to :
object.secondMessage(object.message());
object.secondMessage(object->message());
The import statement
example:
#import <Library.h>
#import "class.h"
Similar to include and import statement in C/C++/Java and no include guards required with Objective-C imports
Method Headers
Example:
-(returnType)methodName:(dataType) param1 //對象方法聲明
Called by:
[object methodName:param1] //對象方法調用
a + rather than - before the method signifies that the method is static //"+"表示靜態方法(class的方法) "-"表示 對象的方法
Similar to :
returnType methodName(dataType param1)
Declaring Inheritance //繼承關係
example:
ClassName:ParentClass<Protocol>
Similar to:
ClassName:ParentClass<Interface>
ClassName extends ParentClass implements interface
Note:
There is no mulitiple inheritance in Objective-C //Objective-C 中不存在多繼承
@property declaration
Used to create getters and setters for to specified variable. Place in the implementation of a class //感覺是getter和setter的一個省事的做法
Example:
@property dataType variablename //在*.h文件中declare 在*.m文件中使用@synthesize與之對應
typical usage of variables defined using @property:
className.variable = value
variable = className.variable
@interface
Declare class member variables, and methods in the interface // @interface 作用是聲明類的變量和方法
@interface ClassName: ParentClass <Protocol>
{
dataType variableName;
}
@property data;
-(returnType)methodName:(dataType)param1
@end
@synthesize
Used to synthesize the getter and setter declared with the @property directive //與@property對應
example:
@synthesize variableName
@implementation
This is where you implement the actual class. //具體實現一個類,或者是類的實現
@implementation ClassName
@synthesize data;
-(returnType)methodName:(dataType)param1
{
....Method Details...
}
@end
Protocols
implemented using <> brackets when creating class interface //在創建interface的時候使用的
example:
@interface ClassName:ParentClass <Protocol>
similar to interface in Java, and C++ //類似Java C++的interface/abstract class 的實現
Self Keyword
Example:
[self message]
similar to :
the this keyword used in Java and C++.
Self must be used when calling a non-static method within the same object // self只能調用非靜態的方法
id keyword
The id keyword is used as generic identifier to identify any class
Similar to the Object keyword in java and to void pointer(void*) in C++ //我的理解是最泛的一個標識
記錄一下,備忘!
2009年11月3日星期二
同步mac iCal ,google calendar and iPod touch
學習一下先進的工作經驗, 剛搞好,配置出乎意料的簡單, 感覺不錯.
參考
http://sammy.hk/2009/02/12/iphone-and-google-calendar-sync?wscr=1680x1050
2009年9月30日星期三
xcode下開發1
這裡使用的是xcode3.0版本,參考
為了閱讀方便,作者把所有的代碼寫到一起了
---------------main.c------------------------
#import
@class UIImageView;
@interface HelloController : UIViewController
{
UIImageView *contentView;
}
@end
@implementation HelloController
- (id)init
{
if (self = [super init])
{
self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
// place any further initialization here
}
return self;
}
/**
*建立起主要的應用程序VIEW
**/
- (void)loadView
{
// Load an application image and set it as the primary view
contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed: @"helloworld.png" ]];
// Provide support for auto-rotation and resizing
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
// Assign the view to the view controller
self.view = contentView;
[contentView release]; // reduce retain count by one
}
// Allow the view to respond to iPhone Orientation changes
/**
* 為自動旋轉功能准備幾個標志
**/
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(void) dealloc
{
// add any further clean-up here
[contentView release];
[super dealloc];
}
@end
/**
* UIApplicationMain()呼叫跑到這裡了
**/
@interface SampleAppDelegate : NSObject
}
@end
@implementation SampleAppDelegate
// On launch, create a basic window
/**
* applicationDidFinishLaunching: 方法
*標准的mainScreen繪制一個新視窗, 它會建立一個導覽控制程序,並知道給一個新的HelloController(UIViewControler子類別)來當作他自己的根源試圖控制程序(Root View Controller)
**/
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[HelloController alloc] init]];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// handle any final state matters here
}
- (void)dealloc {
[super dealloc];
}
@end
/**
main函數是xcode自動產生的,而且還保持未經過人變動的原貌
他呼叫 UIApplicationMain(),並將主要的應用程序的委派方法名稱,這裡是 @"SampleAppDelegate"傳給他!
*/
int main(int argc, char *argv[])
{
/**
*這裡應該是內存分配
**/
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
printf("hellowrold\n");
int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
[pool release];
return retVal;
}
---------------------------------------------
我創建了一個新的PROJECT,然後把上面的代碼copy過去,運行是報錯.屢試不爽.
後來按照書中的要求一步步來,就沒有出錯了,環境不熟悉阿:
記錄一下:
1 刪除classes 文件夾
2 刪除.xib文件
3 在info.list,我的版本上是helloworld-info.list文件中的"NSMainNibFile"的KEY-VALUE 這一行
4 加入image文件夾,這裡是用ADD NEW GROUP
5 拖文件到這個文件夾中(xcode環境中)
6 用上面的main.m覆蓋原來的內容
7 COMMONAD + ENTER 運行模擬器
如果沒有問題的話就OK了
這裡有幾個小技巧:
COMMAND + <
COMMAND + > 模仿IPHONE橫位
COMMAND + SHIHF + R 可以看到後台運行的情況, 我原來一直不知道printf打LOG信息從那裡看,這下子就知道了:)
INSTRUMENTS的執行方式: instruments是APPLE基於SUN的DTRACE搞的,一直很向往之,終於見識了,設定方法:
XCODE環境 -->RUN --> START WITH PERFORMANCE TOOL ,有幾個方式, 我猜OBJECT ALLOCATION 和 CPU SAMPLER應該是比較常用的. 估計以後查內存洩露的時候可以用到.
2009年9月29日星期二
創建一個github
將一些命令記錄一下,備忘:
http://github.com/bangnew/iPhoneDev
bangnew / iPhoneDev
Description: iPhoneDev edit
Homepage: Click to edit edit
Public Clone URL: git://github.com/bangnew/iPhoneDev.git
Your Clone URL:
git@github.com:bangnew/iPhoneDev.git
Global setup:
Download and install Git
git config --global user.name "Your Name"
git config --global user.email bangbangnew@gmail.com
Next steps:
mkdir iPhoneDev
cd iPhoneDev
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:bangnew/iPhoneDev.git
git push origin master
Existing Git Repo?
cd existing_git_repo
git remote add origin git@github.com:bangnew/iPhoneDev.git
git push origin master
Importing a Subversion Repo?
Click here
When you're done:
Continue
-------------------
參考:
Git for OS X
http://code.google.com/p/git-osx-installer/
不錯的文章
http://www.dotblogs.com.tw/billy3321/archive/2009/02/06/7064.aspx
http://zh-tw.whygitisbetterthanx.com/#github
subversion
http://twpug.net/docs/Subversion/