2016-08-23 27 views
-3
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSString *str = [NSString stringWithUTF8String:data.bytes]; 
    NSLog(@"%@",str); 
} 

請轉換這個目標C函數在迅速如何stringWithUTF8String轉換爲迅速

+0

難道你看[代表參考(https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLConnectionDataDelegate_protocol/#//apple_ref/occ/intfm/NSURLConnectionDataDelegate/connection: didReceiveData :)?你讀過這個完美的[Obj-C in Swift](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html)手冊嗎? –

+5

請閱讀http://stackoverflow.com/help/how-to-ask。告訴我們你嘗試過什麼,哪些不起作用。 –

回答

1

它看起來像

func connectDataReceived(connection: NSURLConnection, data: NSData){ 
    let str = NSString(data: data, encoding: NSUTF8StringEncoding)! 
    print(str) 
} 
1

我張貼這是非常簡單的方法的答案,你可以很容易理解。

let strData = "A384CVHJLWHBWRLBRH2348945607275epiuthgthgbpbfvrvdiac15szerqTUDGA89ty757909577257908hbdshjkbhvalasdhbchdcvgcvjvgvaKLOYAQCGHQ" 
let data = strData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! 
let str = NSString(data: data, encoding: NSUTF8StringEncoding)! 
print("\(str)")