iphone之NSString, char, NSData相互转化

iphone之NSString, char, NSData相互转化

NSString *strNSString

const char *pConstChar;

strNSString = [[NSString alloc] initWithUTF8String:pConstChar];

pConstChar = [strNSString UTF8String];


1. NSString转化为UNICODE String:

(NSString*)fname = @“Test”;
char fnameStr[10];
memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
2. NSString转化为char
(NSString*)fname = @“Test”;
char fnameStr[10];
fnameStr =[fname UTF8String];
3. char -> NSData:
 方法一:
   char * postData = "TEST";
   NSData *data = [NSData dataWithBytes:postData length:strlen(postData)];
 方法二:
    转换为NSString: - (id)initWithUTF8String:(const char *)bytes
    然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
4. NSData ->char
  NSData returnData ;
  char* bu=[returnData bytes];
5. NSData->NSString
 NSString* aStr;
 aStr = [NSString alloc] initWithData:aData encoding:NSASCIIStringEncoding];
6. NSString->NSData
 NSData* aData;
 aData = [aStr dataUsingEncoding: NSASCIIStringEncoding];

iphone中按Home键退出程序

  (2012-01-06 15:58:01)
iphone中按Home键退出程序:
1、按Home键的触发事件:
在对应的AppDelegate的方法
- (void)applicationWillResignActive:(UIApplication *)application
2、退出程序:
[application terminateWithSuccess];
或[[UIApplication sharedApplication] terminateWithSuccess];
或exit(0);

折磨人的两个小问题

  (2011-12-13 21:01:50)

NO.1

int i = 2;

NSLog(@"Selected index:%@",i);

结果:Selected index(null)或者直接crash

解决方法:NSLog(@"Selected index:%i",i);

NO.2

cellForRowAtIndexPath中                 

for (UIButton *view in subviews) {

               [view addTarget:self action:@selector(filterSelected:)

forControlEvents:UIControlEventTouchUpInside]; 

}


 

-(IBAction)filterSelected:(id)sender

{

    

}

结果:sender为空(0x0)

原因:view已经被销毁

解决方法:view设成property


补充(NO.2): 

CGRect frame =CGRectMake(i*62,line*62,5252);

UIButton *btn = [[UIButton allocinitWithFrame:frame];

 [btn setBackgroundColor:[UIColor colorWithPatternImage:[UIImageimageNamed:@"Movie_detail_button_episode_number.png"]]];

 [btn addTarget:self action:@selector(onEpisodeSelected:)forControlEvents:UIControlEventTouchUpInside];

[btn setTitle:[NSString stringWithFormat:@"%d", line*numperLine +i]forState:UIControlStateNormal];

这又是正常的

从NIB加载时属性为空的问题

  (2011-12-12 00:32:52)
// This works completely. All outlets works.PhotoShow* obj = [[PhotoShow alloc] init];[[NSBundle mainBundle] loadNibNamed:@"PhotoShow" owner:obj options:nil];// Outlets are always available at this moment.// This works. but does not connects outlets correctly sometimes.PhooShow* obj = [[PhotoShow alloc] initWithNibName:@"PhotoShow" bundle:[NSBundlemainBundle]];// Outlets may not available at this moment.
NSBundle’s methods are the generic API to use for unarchiving NIBs. They do the actual work (together with NSNib).
UIViewController’s initWithNibName:bundle: on the other hand is a way to initialize a view controller which (might) load its view from a nib. The method does not itself load the nib but just takes note of the name. The controller loads the nib lazily when the view is requested.
//试试看这个。。。
int rowToHighlight = [[userDefaults valueForKey:@"content_row"] intValue];
NSIndexPath * ndxPath= [NSIndexPath indexPathForRow:rowToHighlight inSection:0];
[contentTableView scrollToRowAtIndexPath:ndxPath atScrollPosition:UITableViewScrollPositionTop  animated:YES];

Crash with SBJSON library

用了1个多小时解决了这个问题。记录一下,做静态库的时候应该很常见。

http://stackoverflow.com/questions/7854785/crash-with-sbjson-library

http://stackoverflow.com/questions/9462259/adding-spview-to-an-existing-ios-project-just-show-pink-screen-sparrow-framewor


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值