我有使用Swift和Alamofire将我的视频,标题和说明上传到YouTube的工作代码。
我的说明作为一行上传到YouTube,我想在每个变量之后拆分该行。
我的描述变量是这样的:
myDescription = (price! as! String) + " " + (package! as! String)
当该内容发送到YouTube时,它显示为:
" Pricehere PackageName"
我希望PackageName在YouTube描述中显示换行符,如:
" Pricehere
PACKAGENAME"
我在Objective C中对这样一个旧项目做了这个:
NSString *description = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@", str1, str2, str3, str4, str5, str6];
当它传递给YouTube时,它添加了每个变量,然后进行换行。
感谢您的帮助。
编辑添加YouTube上传功能以供参考:
func postVideoToYouTube(token: String, callback: @escaping (Bool) -> Void){
let headers = ["Authorization": "Bearer \(token)"]
let urlYoutube = "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet"
let path = videoURL?.path
let videodata: Data = NSData.dataWithContentsOfMappedFile(path!)! as! Data
upload(
multipartFormData: { multipartFormData in
multipartFormData.append("{'snippet':{'title' : '\(self.myTitle)', 'description': '\(self.myDescription)'}}".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "snippet", mimeType: "application/json")
multipartFormData.append(videodata, withName: "video", fileName: "video.mp4", mimeType: "application/octet-stream")
},
to: urlYoutube,
method:Alamofire.HTTPMethod.post,
headers:headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response)
let result = response.result.value
let JSON = result as! NSDictionary
let videoId = JSON.object(forKey: "id") as! String
print("VideoID: ", videoId)
self.addVideoToPlaylist(videoId: videoId, callback: { (result) in
callback(result)
})
}
break
case .failure(let encodingError):
print(encodingError)
callback(false)
break
}
}
)
}
答案 0 :(得分:0)
小心强制投射像你一样的变量,我建议你做这样的事情:
if let price = price as? String, let package = package as? String {
myDescription = "\(price)\n\(package)"
}
通过这种方式,您可以安全地解包并使用字符串插值来构造字符串,您可以在the documentation中阅读更多内容。