我希望您能将Obj-C转换为Swift,因为我对Obj-C不了解
viewController.h:
#import <UIKit/UIKit.h>
#define kPostURL @"http://localhost/tutorialTest.php"
#define kName @"name"
#define kMessage @"message"
@interface ViewController : UIViewController{
IBOutlet UITextField *nameText;
IBOutlet UITextView *messageText;
NSURLConnection *postConnection;
}
-(IBAction)save:(id)sender;
@end
viewController.m:
-(void) postMessage:(NSString*) message withName:(NSString *) name{
if (name != nil && message !=nil){
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kName, name]];
[postString appendString:[NSString stringWithFormat:@"&%@=%@", kMessage, message]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
}
我希望你们能帮助我翻译成Swift。谢谢
我想这可能会有所帮助。它的翻译不完全正确,但是您可以通过解决编译时和运行时错误来进行修改。
func postMessage(message: String, withName name: String) {
if name != nil && message != nil {
var postString: NSMutableString = NSMutableString.stringWithString(kPostURL)
postString.appendString("?\(kName)=\(name)")
postString.appendString("&\(kMessage)=\(message)")
postString.setString(postString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding))
var request: NSMutableURLRequest = NSMutableURLRequest(uRL: NSURL.URLWithString(postString))
request.setHTTPMethod("POST")
postConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句