使用构造方法创建 http 主体
我正在尝试创建一个 http 主体,我将使用 NSURLRequest post 传递该主体。 我的连接类已全部设置完毕。
问题是我有几种返回 NSStrings 和 UInt32 的方法,以及一种构造方法,我想用它来将所有这些方法放入一个数据类型格式的 http 主体中。
但是,我不确定如何调用这些方法从我的构造方法返回正确的数据,以将数据收集到一个数据对象中。
这是我的一些代码(缩短了所以更清晰)
这些方法用于返回所需的数据
- (UInt32) addDataVer
{
UInt32 dataVer = 0;
return dataVer;
}
- (NSString *) addReg
{
NSString *reg = [[NSString alloc] initWithString:@"abcd1"];
return reg;
}
- (NSString *) addActiv
{
NSString *activ = [[NSString alloc] initWithString:@"abcd2"];
return activ;
}
从这里我不知道该怎么做,或者如何获取数据。我创建了一个构造方法,我想用它来获取数据,然后我想使用该数据构建一个 NSData 对象,在其中按顺序将返回的数据放入其中。
这是我的构造类,
- (void) constructRequest
{
//what the heck do I call in here? lol
}
我要做的最后一件事是弄清楚如何将每个返回值的 nsdata 表示放入数据对象中...如果这有意义...
任何帮助将不胜感激。
更新::
所以我想出了如何通过遵循力量将返回值放入我的构造方法中!
- (void) constructRequest
{
NSString *mystring = [[NSString alloc] initWithString:[self addReg]];
NSLog(@"mystring %@", mystring);
}
但是我不确定如何使用返回的 UInt32 来执行此操作,或者如何将其转换为 NSData 结构
I am trying to create a http body that I am going to pass in using NSURLRequest post.
I have my connection class all set up.
The thing is i have several methods that return NSStrings and UInt32's and one construction method that I want to use to put all of these methods into one http body which will be of data type format.
However I'm not sure how to call these methods that return the correct data from my construction method to gather the data into one data object.
here is some code that I have (shortened so its a little clearer)
these methods are used to return the data needed
- (UInt32) addDataVer
{
UInt32 dataVer = 0;
return dataVer;
}
- (NSString *) addReg
{
NSString *reg = [[NSString alloc] initWithString:@"abcd1"];
return reg;
}
- (NSString *) addActiv
{
NSString *activ = [[NSString alloc] initWithString:@"abcd2"];
return activ;
}
from here I'm not sure what to do, or how to get the data. I have created a construction method, that I want to use to grab the data and then I want to use that data to build a NSData object where I put the returning data into it in order.
this is my construction class
- (void) constructRequest
{
//what the heck do I call in here? lol
}
the last thing I will have to do is figure out how to put the nsdata representation of each return value into the data object... if that makes sense...
any help would be appreciated.
UPDATE::
So I figured out how to get the return value into my construction method, by following the force!
- (void) constructRequest
{
NSString *mystring = [[NSString alloc] initWithString:[self addReg]];
NSLog(@"mystring %@", mystring);
}
however I am not sure how to do this with a returning UInt32, or how to convert this in to a NSData structure
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自关于字符串格式的 Apple 文档 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
[NSString stringWithFormat:"my unsigned 32-bit int: %d", [self addDataVer]];
来自 Apple 文档关于 NSString 类
将整个字符串转换为数据:
[myNSString dataUsingEncoding:NSUTF8StringEncoding];
From Apple Docs on String formatting https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
[NSString stringWithFormat:"my unsigned 32-bit int: %d", [self addDataVer]];
from Apple Docs about NSString class
To convert your entire string to data:
[myNSString dataUsingEncoding:NSUTF8StringEncoding];