2015年2月9日 星期一

好用的.h property用 convenient method 提供value

.h

// convinent methods
@property (readonly, nonatomic) BOOL isConnected;


.m

-(BOOL)isConnected
{
    return self.currentPeripheral.state == CBPeripheralStateConnected;
}


其實也就是複寫isConnected由property自動生成的getter method啦!

.h的readonly property怎麼write?

對外readonly而內部要write怎辦?

只要在m裡面加上同名的property就好,或是寫上 _propertyNam,前面加上_

原理是加上 _就等於直接存取instance verbel,所以就可以寫值了

平常的dot寫法不行或是[self setPropertyName]不行是因為上面兩種其實都是call method 去寫的,也就是其實是語法糖衣,當你h的宣告為readonly,編譯器不會自動幫你寫上setter,所以用上面兩種不行!

需要直接用instance verbel做寫入~


---------------

當你寫property後,編譯器已經自動幫你寫好setter和getter,還有synthesize ~ 所以可以直接用_去訪問instance verbal.

2014年6月14日 星期六

d013

#include
#include
#include

int main(void) {
    char s[999];
    unsigned long int store[100];
    int num;
    int i;
    while( gets(s)!=0 ) {
        memset(store, 0, 100);
        num = atoi(s);
        store[0]=1;
        store[1]=1;
        for (i=2; i
            store[i] = store[i-2]+store[i-1];
        }
        printf("%lu\n",store[num-1]);
    }
    return 0;

}

tip:
1.當數字太大時的儲存與表示方法
http://stackoverflow.com/questions/3209909/how-to-printf-unsigned-long-in-c

d039

#include
#include
#include

int main(void) {
    char s[999];
    int num;
    int i;
    while( gets(s)!=0 ) {
        num = atoi(s);
        for (i=2; i<=sqrt(num); i++) {
            if(num % i == 0){
                printf("not prime\n");
                break;
            }
        }
        if (i>sqrt(num)) {
           printf("prime\n");
        }
    }
    return 0;

}

d012

#include
#include

int main(void) {
    char s[999];
    char s2[999];
    int stelen;
    int i;
    int count;
    while( gets(s)!=0 ) {
        count = 0;
        memset(s2, 0, 999);
        stelen = strlen(s);
        for (i=stelen-1; i>=0; i--) {
            s2[count] = s[i];
            count++;
        }
        if(!strcmp(s,s2)){
            printf("Yes\n");
        }else printf("No\n");
    }
    return 0;

}

tips:
1.單純結合d001與d011
2.ISO C90 forbids mixed declarations and code 變數不要宣告在太後面的地方,最好在邏輯之前。
3.一樣記得同樣的char array你在去宣告一次並不會變成新的array,要重複使用就要用memset去empty array。

d011

#include
#include
int main(void) {
    char s[999];
    int stelen = 0;
    int spacenum;
    int i = 0;
    while( gets(s)!=0 ) {
        char s1[100];
        char s2[100];
        memset(s1, 0, 100);
        memset(s2, 0, 100);
        stelen = strlen(s);
        spacenum = strcspn(s, " ");
        for (i=0; i
            s1[i] = s[i];
        }
        for (i=spacenum+1; i
            s2[i-(spacenum+1)] = s[i];
        }
        if(!strcmp(s1,s2)){
            printf("Yes\n");
        }else printf("No\n");
    }
    return 0;

}

tips:
1.記得輸出要換行...\n
2.記得char array每次都要重新清空,就算char宣告放在while裡也一樣,不會像其他語言這樣可以重新init,在c可使用memset重設值。

source:
memset 設定位元組中的位元值,設定的方式從s 開始將n 個位元組設定成為c 的位元值並回傳s,經常運用的範圍是在將位元組的位元值清為0。
http://stackoverflow.com/questions/1559487/how-to-empty-a-char-array

d001

#include
#include
int main(void) {
    char s[999];
    int stelen = 0;
    int i = 0;
    while( gets(s)!=0 ) {
        stelen = strlen(s);
        for (i=stelen-1; i>=0; i--) {
            if(i!=0)printf("%c", s[i]);
            else printf("%c\n", s[i]);
        }
    }
    return 0;

}

tips:
1.for loop裡面不能宣告變數(ex: int i),似乎與c99有關
2.不能用scanf,測資裡有space,scant讀到space就讀完了。所以用gets。
3.scanf()讀取結尾是回傳EOF;gets()則是回傳0。

2012年12月20日 星期四

depoly inotify-tools error : Protected multilib versions: glibc-2.12-1.80.el6.i686 != glibc-2.12-1.80.el6_3.6.x86_64


depoly inotify-tools時出現以下error :

Error: Protected multilib versions: glibc-2.12-1.80.el6.i686 != glibc-2.12-1.80.el6_3.6.x86_64
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
glibc-common-2.12-1.80.el6_3.6.x86_64 is a duplicate with glibc-common-2.12-1.80.el6.x86_64
glibc-common-2.12-1.80.el6_3.6.x86_64 has missing requires of glibc = ('0', '2.12', '1.80.el6_3.6')

solution:

yum remove glibc-2.12-1.80.el6_3.6.x86_64
接著再install一次

SCOM linux agent deploy error : ssl certificate error

當deploy SCOM linux agent時,出現ssl certificate error時,refer以下網址

http://technet.microsoft.com/en-us/library/hh212851.aspx

另外如果安裝完成後執行出現access is  denied,為Run as account設定上面問題,refer

http://blogs.technet.com/b/kevinholman/archive/2012/03/18/deploying-unix-linux-agents-using-opsmgr-2012.aspx

search: run as account

2012年10月12日 星期五

Nginx搭配Apache做balance與proxy與避免ip直接訪問

#所有php的动态页面均交由apache处理
location ~ .(php)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:88;
}
#所有静态文件由nginx直接读取不经过apache
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
ref: (http://www.ha97.com/5119.html)

一些觀念
ref : (http://www.itlearner.com/article/4508)

實作補充
ref : (http://stackoverflow.com/questions/3434182/apache-and-ultimate-config-for-nginx-to-serve-all-virtual-hosts-in-the-right-way)

//避免直接ip訪問
在nginx/conf.d/default.conf裡加上並寫上自己對應的rewrite rule ex:http://yourdomain.com$request_uri?


server {
        listen 80 default_server;#ip
        server_name ip;
        rewrite ^ http://mrshih.com$request_uri?;
}






2012年9月12日 星期三

升級到xcode4.5後facebook sdk有遇到問題

問題如下

Ld /Users/shih/Library/Developer/Xcode/DerivedData/food-csyywpmvwokybxdzxglwpmdzfgug/Build/Intermediates/food.build/Debug-iphoneos/food.build/Objects-normal/armv7s/food normal armv7s
    cd /Users/shih/Desktop/food
    setenv IPHONEOS_DEPLOYMENT_TARGET 5.0
    setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch armv7s -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk -L/Users/shih/Library/Developer/Xcode/DerivedData/food-csyywpmvwokybxdzxglwpmdzfgug/Build/Products/Debug-iphoneos -F/Users/shih/Library/Developer/Xcode/DerivedData/food-csyywpmvwokybxdzxglwpmdzfgug/Build/Products/Debug-iphoneos -F/Users/shih/Desktop/food/../../Documents/FacebookSDK -filelist /Users/shih/Library/Developer/Xcode/DerivedData/food-csyywpmvwokybxdzxglwpmdzfgug/Build/Intermediates/food.build/Debug-iphoneos/food.build/Objects-normal/armv7s/food.LinkFileList -dead_strip -lsqlite3.0 -fobjc-arc -fobjc-link-runtime -miphoneos-version-min=5.0 -framework MapKit -framework MobileCoreServices -framework CoreLocation -framework QuartzCore -framework UIKit -framework Foundation -framework CoreGraphics -framework FacebookSDK -o /Users/shih/Library/Developer/Xcode/DerivedData/food-csyywpmvwokybxdzxglwpmdzfgug/Build/Intermediates/food.build/Debug-iphoneos/food.build/Objects-normal/armv7s/food

ld: file is universal (3 slices) but does not contain a(n) armv7s slice: /Users/shih/Documents/FacebookSDK/FacebookSDK.framework/FacebookSDK for architecture armv7s
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解決辦法

專案 => TARGET => Build Settings => Vaild Architectures => armv7 armv7s

把armv7 armv7s 改成 armv7








2012年9月2日 星期日

在storyboard下使用scroll view如何視覺化編輯畫面

1.
選擇controller的Inspector標籤
size屬性選擇Freeform

2.
選擇scroll view的Inspector標籤的右邊那個標籤
看到view那裏,編輯你要的width, height
接著就會看到storyboard上的controller的可編輯範圍出現變化了

3.
接著設計你的view
然後outlets scrollview的controller程式碼上,設定你在storyboard上的width, height

[self.sc setContentSize:CGSizeMake(320,600)];

DONE.

參考來源:
http://stackoverflow.com/questions/9288070/visually-arrange-subviews-in-a-large-uiscrollview

2012年8月28日 星期二

MBProgressHUD 方便建造thread, 並且不檔到navigation item(比如back)

http://github.com/matej/MBProgressHUD

.h檔部分
#import "MBProgressHUD.h"  
.....{
     
    MBProgressHUD *HUD;
}

.m檔部分
- (voidviewDidLoad{
    
    [super viewDidLoad];

    HUD = [[MBProgressHUD alloc] initWithView:self.tv];
    // 這裡是navigation的item如back在有loading image時可以用的關鍵,記住initWithView不可以指定  self.view,因為這樣以來loading會覆蓋過整個view,關鍵是要另外指定一個較小不是全範圍的view如table view來init.
    [self.tv addSubview:HUD]; 
    // 這裡則是要指定那個較小的view去做addSubview:HUD,如此以來執行的時候也可以按navigation的item了

    HUD.delegate = self;  
    HUD.labelText = @"Loading";  
    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}

- (void) myTask {  
     // Do something usefull in here instead of sleeping ...
     // 背景運作的code如網路載入資料等放這...
}

- (void)hudWasHidden { 
    // Remove HUD from screen when the HUD was hidded  
    [HUD removeFromSuperview];
}

2012年8月20日 星期一

兩個view controller資料傳送呼叫顯示等


---------------------------------------------------------------------------

1 呼叫 2 出來
[self performSegueWithIdentifier:@"upload to location" sender:nil];

---------------------------------------------------------------------------

1 傳送data到 2

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id dv = segue.destinationViewController;
    [dv setValue:@"123,111" forKey:@"gps"];
}

---------------------------------------------------------------------------

---------------------------------------------------------------------------
2 返回 1 (不透過nav bar 的back button)


    UIViewController *prevVC = [self.navigationController.viewControllers objectAtIndex:1];//1 is level
    [self.navigationController popToViewController:prevVC animated:YES];

指定回去的leval,1就是leavl
---------------------------------------------------------------------------


2 傳送data回 1 (需要實作delegate)

step1 :(在page2的.h檔定義delegate)



//--delegate部分
@protocol page2Delegate <NSObject>

@optional
- (void)passValue:(NSString *)value;

@end
//--

//--
@property (nonatomic, weak) id<LocationDelegate> delegate;
//--

---------------------------------------------------------------------------

step2 : 在page1裡載入delegate

#import "page2.h"//實作協定用

@interface uploadViewController : UIViewController<page2Delegate>


並在page1.m檔裡面實作delegate 方法

- (void)passValue:(NSString *)value{
     //在這裡實作協定
}

---------------------------------------------------------------------------

strp3:在pag1呼叫page2時,指定自己是page2的delegate


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id dv = segue.destinationViewController;
    [dv setValue:self forKey:@"delegate"];
}
---------------------------------------------------------------------------
step4 :在page2適當的call method,讓delegate執行這個method

[self.delegate passValue:@"pass from 2 data string"];


////類似(  performSegueWithIdentifier + prepareForSegue )

    /*
    UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
    locationViewController *uploadView = [storybord instantiateViewControllerWithIdentifier:@"location"];
    [self.navigationController pushViewController:uploadView animated:YES];
     */





2012年4月2日 星期一

Video Locker - iPhone iPod


Lock video from Apple Photos app !

Features:
。One Second Lock : press home button , lock Immediately.
。Smart Lock technology : if you forget to turn off the app , it auto-lock your privacy.
。High Quality engine :No compressed , Perfect storage.
。AirPlay Support.
。Total Solution : Import , lock , delete , export , copy.
。Full-featured Video Player
。Import video from your iPod iPhone.
。Easy to use interface and operation.

Note:
Because of Apple's protection for user privacy, we can not delete video from an album locally, You must delete them yourself, after importing into Video Lock.

How to let users to make better use of the software is our obligation, Bath import photo features use special technical
which Apple requires the user allow to open Location Service.

Email : daan.shih@gmail.com

2012年3月31日 星期六

http://stackoverflow.com/questions/6568210/how-to-save-video-from-assets-url
http://stackoverflow.com/questions/4545982/getting-video-from-alasset

2012年3月28日 星期三

控制storyboard , reload storyboard , segue

好用參考來源
http://ryan.easymorse.com/?p=39

---------------------------

要使每次載入都回到login
可在applicationWillEnterForeground加上



    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    
    [self.window makeKeyAndVisible];

這樣可以reload整個storyboard,等於跟剛開啟一樣

-------------------

還有控制segue
    
    [self performSegueWithIdentifier:@"push to photolibrary" sender:nil];

詳細猜考來源

--------------------

直接載入特定storyboard

    UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    [self presentViewController:[storybord instantiateViewControllerWithIdentifier:@"PhotoLibrary"] animated:NO completion:nil];





如何製作圓角tableview, 加上textfield


    tableview.dataSource = self;
    tableview.delegate = self;
   
    //tableview 圓角 邊框 分隔線 coustom
    //圓角
    tableview.layer.cornerRadius = 13.0;
    tableview.layer.masksToBounds = YES;
    //分隔線
    [self.tv setSeparatorColor:[UIColor lightGrayColor]];
    //邊框
    tableview.layer.borderWidth = 1;
    tableview.layer.borderColor = [[UIColor lightGrayColor] CGColor];
   

    接著製作cell  並在裡面加上textfield
   
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tv dequeueReusableCellWithIdentifier:CellIdentifier];
   
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    // Configure the cell...
    if([indexPath row]==0){

        //這是原本的cell text label

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *tmp = [defaults objectForKey:@"question"];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
        cell.textLabel.text = [NSString stringWithFormat:@"%@%@", @"密碼保護問題 : ", tmp];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if ([indexPath row]==2) {

        /這是textfield      

        u3 = [[UITextField alloc] initWithFrame:CGRectMake(9.5, 10, 300, 60)];
        u3.placeholder = @"新密碼";
        u3.returnKeyType = UIReturnKeyDone;
        u3.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell addSubview:u3];      
    }

    依此類推......

}

避免textfield被鍵盤檔到

http://www.cppblog.com/kongque/archive/2011/08/24/154256.html 簡單易懂的教學

2012年3月21日 星期三

push到storyboard的view上

給 storyboard 的 view 一個Identifier

UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

[self presentViewController:[storybord instantiateViewControllerWithIdentifier:@"PhotoLibrary"] animated:NOcompletion:nil];

粗體 => 取得view

--------------------------------------------------------------------------------------

建立一個segue 並加上idIdentifier
就可利用以下方法任意觸發

[self performSegueWithIdentifier:@"touch to push" sender:nil];


資料來源:http://ryan.easymorse.com/?p=39