在ns中将nsMutablestring转换为nsdata

时间:2014-01-24 06:18:56

标签: ios nsdata

我是iOS开发的新手我正在尝试转换NSdata中的追加但我无法进行转换我尝试了一些代码。在我的Nsmutable String中,我有一个url链接,其中包含我正在传递字符串形式json的视频,所以现在我想附加NSdata来显示该视频网址

这是传递url链接的NSmutablesring的代码

-(void)setDataSource:(vedios *)inVideosObj
{

    self.titile.text = inVideosObj.title;

    url =[NSURL URLWithString:inVideosObj.video];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.responseData = [[NSMutableString alloc]init];

}

NSMutablestring appeand编码:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{

    NSData *mydata =[self.responseData dataUsingEncoding:NSUTF16StringEncoding];

    [self.responseData appendString:data];



}

2 个答案:

答案 0 :(得分:3)

NSString to NSData

NSString* strResult = @"yourstring";
NSData* dataResult = [strResult dataUsingEncoding:NSUTF8StringEncoding];

NSData to NSMutableString

NSMutableString *jsonStr = [[NSMutableString alloc] initWithData:response encoding:NSUTF8StringEncoding];

NSLog(@"%@",jsonStr);

答案 1 :(得分:0)

您正在将responseData初始化为NSMutableString。将其初始化为NSMutableData

@property (nonatomic, weak) NSMutableData * responseData;

现在在

-(void)setDataSource:(vedios *)inVideosObj
{
    //Your work
    self.responseData = [NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
    [self.responseData appendData: data];

}

如果您愿意,请将此self.responseData转换为NSString

NSString * responseString = [NSString alloc] initWithData: self.responseData encoding: NSUTF16StringEncoding]

享受.. !!