NSdata 與 NSString,Byte數組,UIImage 的相互轉換html
原文網址:http://www.cnblogs.com/jacktu/archive/2011/11/08/2241528.htmljava
1. NSData 與 NSString
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
NSString->NSData
NSString *aString = @"1234abcd";
NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
2.NSData 與 Byte
NSData-> Byte數組
NSString *testString = @"1234567890";
NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];
Byte *testByte = (Byte *)[testData bytes];
for(int i=0;i<[testData length];i++)
printf("testByte = %d\n",testByte[i]);python
Byte數組-> NSData
Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
Byte數組->16進制數
Byte *bytes = (Byte *)[aData bytes];
NSString *hexStr=@"";
for(int i=0;i<[encryData length];i++)
{
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff]; ///16進制數
if([newHexStr length]==1)
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
else
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
NSLog(@"bytes 的16進制數爲:%@",hexStr);
16進制數->Byte數組
///// 將16進制數據轉化成Byte 數組
NSString *hexString = @"3e435fab9c34891f"; //16進制字符串
int j=0;
Byte bytes[128]; ios
NSData生成:git
NSDictionary *dic =[NSDictionary dictionaryWithObject:@"hello" forKey:@"KEY"];
NSData *d = [NSKeyedArchiver archivedDataWithRootObject:dic];
從文件生成NSData:github
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"hello" ofType:@"png"];
NSData *d = [[NSData alloc] initWithContentsOfFile: path];
取得元素長度:objective-c
int i = [d length];數組
NSData型轉成NSDictionary型:xcode
NSDictionary *reverse = [NSKeyedUnarchiver unarchiveObjectWithData: d];
NSData *data = [NSData dataWithContentsOfFile:filePath]; NSUInteger len = [data length]; Byte *byteData = (Byte*)malloc(len); memcpy(byteData, [data bytes], len);
NSString *strPath = @"/Users/user/Desktop/jkk.txt";
NSLog(@"string = %@",[[NSString alloc]initWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:Nil]);
NSData *strData = [[NSData alloc]initWithContentsOfFile:strPath];
NSLog(@"size = %d字節; strData = %@",strData.length,strData.description);
NSUInteger len = [strData length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [strData bytes], len);
for (int i = 0; i < len; i++) {
printf("%c",byteData[i]);
}
NSData *Data222 = [[NSData alloc]initWithBytes:byteData length:len];
NSLog(@"Data222 : %@",Data222);
NSRange rangeData = {0,3};
NSData *subData = [strData subdataWithRange:rangeData];
NSLog(@"subData : %@",subData);
NSData *Data333 = [[NSData alloc]initWithData:subData];
NSLog(@"Data333: %@",[[NSString alloc]initWithData:Data333 encoding:NSUTF8StringEncoding]);ruby
原文網址:http://stackoverflow.com/questions/30608669/how-to-turn-4-bytes-into-a-float-in-objective-c-from-nsdata
NSData* data = [NSData dataWithContentsOfFile:@"/tmp/java/test.dat"]; int32_t bytes; [data getBytes:&bytes length:sizeof(bytes)]; bytes = OSSwapBigToHostInt32(bytes); float number; memcpy(&number, &bytes, sizeof(bytes)); NSLog(@"Float %f", number);
NSString與int和float的相互轉換
NSString *tempA = @"123";
NSString *tempB = @"456";
1,字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
2,字符轉int
int intString = [newString intValue];
3,int轉字符
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
4,字符轉float
float floatString = [newString floatValue]
5,float轉字符
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
NSTimer攜帶傳遞值
NSTimer有個屬性 叫userInfo,下面的方法的第四個參數userInfo
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
userinfo用NSTimer的實例能夠得到,返回類型是 id
[timer userInfo];
[NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(handleTimer:) userInfo:@"參數" repeats:YES];
用的時候只要在下面函數裏調用強制轉換的userinfo就行,
-(void)handleTimer:(NSTimer*)timer
{
//這裏使用(NSString *)[timer userInfo]
}
-(void)handleTimer:(NSTimer*)timer
這裏最好用id作參數
-(void)handleTimer:(id)timer
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if(oldView != nil){
[dict setObject:oldView forKey:@"oldView"];
}
if(newView != nil){
[dict setObject:newView forKey:@"newView"];
}
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(onTimer:) userInfo:dict repeats:NO];
[dict release];
- (void)onTimer:(NSTimer *)timer {
UIView *oldView = [[timer userInfo] objectForKey:@"oldView"];
UIView *newView = [[timer userInfo] objectForKey:@"newView"];
[UIView animateWithDuration:2.0 delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
oldView.alpha = 0.0;
newView.alpha = 1.0;
}
4.
在學習OC代碼的時候,編譯oc .m文件有以下兩種方式(1) clang -fobjc-arc -framework Foundation Hello.m -o hello.out
./hello.out
(2) 用xcode編譯.m後生成的可執行文件hello和源碼hello.m不在同一個目錄
經過右擊 hello, show in finder能夠找到hello可執行文件
或者經過下面的代碼查看
To get the path to your file use
NSString * myFile = [[NSBundle mainBundle]pathForResource:@"Alert" ofType:@"txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: myFile ]){
NSLog(@"good");
}
這個問題是在學習NSFileManger的時候,使用NSFileManger建立的文件,在源碼的文件夾中找不到,
上面的方式能夠查看到實際的路徑:
/Users/yqf/Library/Developer/Xcode/DerivedData/NSFileManagerTest2-eaeuwbogilljalgvxymvlvdqwjwb/Build/Products/Debug/NSFile
5. Xcode workSpace與Project配置
原文網址:http://blog.sina.com.cn/s/blog_13edf9b370102w8r4.html
.xcodeproj
的工程配置文件,而不是選擇整個工程的父文件夾。注意點1 拖到面板的時候,鼠標要放在最上方,否則會致使工程
注意點2 只能託後綴名爲.xcodeproj
的工程配置文件
6. 用NSScanner判斷String內容是否爲整型或者浮點型數據
原文網址:http://cocoa-chen.github.io/blog/2014/10/27/yong-nsscannerpan-duan-stringnei-rong-shi-fou-wei-zheng-xing-huo-zhe-fu-dian-xing-shu-ju/
1
2 3 4 5 6 7 8 |
- (BOOL)isPureInt:(NSString *)string{ if (!string) { return NO; } NSScanner *_scanner = [NSScanner scannerWithString:string]; int val; return [_scanner scanInt:&val] && [_scanner isAtEnd]; } |
1
2 3 4 5 6 |
NSString *pureIntString = @"123"; NSString *noPureIntString = @"123ab"; BOOL isPure1 = [self isPureInt:pureIntString]; NSLog(@"'%@'字符串%@int內容的字符串",pureIntString,isPure1 ? @"是" : @"不是"); BOOL isPure2 = [self isPureInt:noPureIntString]; NSLog(@"'%@'字符串%@int內容的字符串",noPureIntString,isPure2 ? @"是" : @"不是"); |
1
2 3 4 5 6 7 8 |
- (BOOL)isPureFloat:(NSString *)string{ if (!string) { return NO; } NSScanner* scan = [NSScanner scannerWithString:string]; float val; return [scan scanFloat:&val] && [scan isAtEnd]; } |
1
2 3 4 5 6 |
NSString *pureFloatString = @"1.23"; NSString *nopureFloatString = @"1.23a"; BOOL isPure1 = [self isPureFloat:pureFloatString]; NSLog(@"'%@'字符串%@float內容的字符串",pureFloatString,isPure1 ? @"是" : @"不是"); BOOL isPure2 = [self isPureFloat:nopureFloatString]; NSLog(@"'%@'字符串%@float內容的字符串",nopureFloatString,isPure2 ? @"是" : @"不是"); |
7. Objective-C 拆分字符串成數組(相似java 的 split)
原文網址:http://blog.csdn.net/sirodeng/article/details/8306288
在不少語言如 java , ruby , python中都有將字符串切分紅數組或者將數組元素以某個間隔字符串間隔造成新的數組。 其實NSArray也提供了這樣的功能。
使用-componentsSeparatedByString:來切分NSArray。 如:
用-componentsJoinedByString:來合併NSArray中的各個元素並建立一個新的字符串,如:
string = [aArray componentsJoinedByString:@","];
這樣,上面的數組就中的各個元素就以」,」分割造成一個字符串。
8. ios 開發 NSArray 排序 (根據NSString的值排序)
原文網址:http://blog.csdn.net/wenluma/article/details/8705272
以上包含了有從小到大的排序,也包含有大到小的排序
若是是針對字符串的排序,有更好的方法,
[cpp] view plain copy
8. 判斷NSString是否包含某個字符串
int i = 1; NSData *data = [NSData dataWithBytes: &i length: sizeof(i)];
int i; [data getBytes: &i length: sizeof(i)];