我Codable第一次使用Swift 4的协议,我无法理解使用decodeIfPresentfrom Decodable.
/// Decodes a value of the given type for the given key, if present.
///
/// This method returns `nil` if the container does not have a value associated with `key`, or if the value is null. The difference between these states can be distinguished with a `contains(_:)` call.
///
/// - parameter type: The type of value to decode.
/// - parameter key: The key that the decoded value is associated …Run Code Online (Sandbox Code Playgroud) 我正在尝试升级我的项目代码并发现此警告
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteRowAction = UITableViewRowAction(style: .destructive, title: deleteActionTitle) { [unowned self] (_, indexPath) in
//code you want to execute }
return [deleteRowAction]
}
Run Code Online (Sandbox Code Playgroud) 我刚刚转换了我的小应用程序,但我发现了这个错误:'substring(from :)'已被弃用:请使用字符串切片下标和'partial range from'运算符
我的代码是:
let dateObj = dateFormatterFrom.date(from: dateStringa)
if dateObj != nil {
cell.detailTextLabel?.text = dateFormatterTo.string(from:(dateObj!))
} else {
let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = thisRecord.pubDate.substring(from: index)
}
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中有一个自定义相机,它工作正常,但在新的更新后,我收到此错误:
'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer :)'在iOS 11.0中已弃用:请改用 - [AVCapturePhoto fileDataRepresentation].
这是我收到错误的行:
guard let imageData =
AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
return
}
Run Code Online (Sandbox Code Playgroud)
这是我的全部功能(如果需要):
//Take pic function
func photoOutput(_ captureOutput: AVCapturePhotoOutput,
didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
resolvedSettings: AVCaptureResolvedPhotoSettings,
bracketSettings: AVCaptureBracketedStillImageSettings?,
error: Error?) {
// Make sure we get some photo sample buffer
guard error == nil,
let photoSampleBuffer = photoSampleBuffer else {
print("Error capturing photo: \(String(describing: error))")
return
}
// Convert photo same buffer to a jpeg image data by using // …Run Code Online (Sandbox Code Playgroud) 我已定义enum如下:
enum Type: String, Codable {
case text = "text"
case image = "image"
case document = "document"
case profile = "profile"
case sign = "sign"
case inputDate = "input_date"
case inputText = "input_text"
case inputNumber = "input_number"
case inputOption = "input_option"
case unknown
}
Run Code Online (Sandbox Code Playgroud)
映射JSON字符串属性.自动序列化和反序列化工作正常,但我发现如果遇到不同的字符串,反序列化将失败.
是否可以定义一个unknown映射任何其他可用案例的案例?
这可能非常有用,因为这些数据来自RESTFul API,可能会在将来发生变化.
尝试获取String的子字符串并将其附加到字符串数组:
var stringToSplit = "TEST TEXT"
var s = [String]()
let subStr = anotherString[0 ..< 6]
s.append(subStr) // <---- HERE I GET THE ERROR
Run Code Online (Sandbox Code Playgroud)
相近
PhotoPicker发现错误:错误Domain = PlugInKit Code = 13
还有
https://forums.developer.apple.com/thread/82105
但我已经尝试了所有这些建议,但仍然在调试日志中出错.运行Swift 4 XCode 9A235
各个地方的建议是......
我没有在Swift 3中遇到这个问题 - 以前的xcode.但是使用Swift 4,我尝试了每次看到的建议,但仍然会出现以下错误
[发现]发现扩展时遇到的错误:错误域= PlugInKit代码= 13"查询已取消"UserInfo = {NSLocalizedDescription =查询已取消}
选择器工作正常,我最终选择了照片中的图像,但每次都在选择器退出(取消或选择)时收到此错误消息...
有任何建议如何停止错误消息?除了其他两个链接提供的事项列表(上面概述)
我的方法
@objc internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imageSelected = nil
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
imageSelected = editedImage
} else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
imageSelected = …Run Code Online (Sandbox Code Playgroud) 似乎在Swift 4.1 flatMap中已弃用.然而,Swift 4.1中 有一个新方法,compactMap它做同样的事情?有了flatMap你可以集合中变换的每个对象,然后删除在无任何项目.
像flatMap一样
let array = ["1", "2", nil]
array.flatMap { $0 } // will return "1", "2"
Run Code Online (Sandbox Code Playgroud)
就像compactMap一样
let array = ["1", "2", nil]
array.compactMap { $0 } // will return "1", "2"
Run Code Online (Sandbox Code Playgroud)
compactMap 正在做同样的事情.
这两种方法有什么区别?为什么Apple决定重命名该方法?
let items: [String] = ["A", "B", "A", "C", "A", "D"]
items.whatFunction("A") // -> [0, 2, 4]
items.whatFunction("B") // -> [1]
Run Code Online (Sandbox Code Playgroud)
Swift 3是否支持这样的功能whatFunction(_: Element)?
如果没有,最有效的逻辑是什么?