在Xcode 9和Swift 4中,我总是会收到一些IBInspectable属性的警告:
@IBDesignable public class CircularIndicator: UIView {
// this has a warning
@IBInspectable var backgroundIndicatorLineWidth: CGFloat? { // <-- warning here
didSet {
backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
}
}
// this doesn't have a warning
@IBInspectable var topIndicatorFillColor: UIColor? {
didSet {
topIndicator.fillColor = topIndicatorFillColor?.cgColor
}
}
}
Run Code Online (Sandbox Code Playgroud)
有办法摆脱它吗?
我正在使用UICollectionView但未找到以下方法:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
Run Code Online (Sandbox Code Playgroud)
请建议我替换它.
UITableViewDelegate.h
// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
// return nil to get the default swipe actions
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos);
Run Code Online (Sandbox Code Playgroud)
但是,我在trailingActions方法中返回nil,我仍然可以在tableview中完全滑动删除.如何防止完全滑动?(我希望用户必须滑动然后按"删除"按钮.
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return nil
}
Run Code Online (Sandbox Code Playgroud)
编辑:我已经实现了canEditRowAt并在iOS 11/XCode 9/Swift 4更新之前提交了编辑样式.即使在我实现trailingSwipeActionsConfigurationForRowAt之前,也启用了完全滑动.
以下代码给出了编译错误
'WeakReference'要求'ServiceDelegate'是类类型
protocol ServiceDelegate: AnyObject {
func doIt()
}
class SomeClass() {
// compile error occurs in this line
private var observers = [WeakReference<ServiceDelegate>]()
}
Run Code Online (Sandbox Code Playgroud)
WeakReference代码:
final class WeakReference<T: AnyObject> {
private(set) weak var value: T?
init(value: T?) {
self.value = value
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?应根据此站点正确声明委托.
到目前为止我尝试了什么:
AnyObject,以
class不解决问题.我做了一个核心数据模型:
class Order: NSManagedObject {
... //code
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试构建时出现错误:
在这种情况下,“顺序”对于类型查找来说不明确
使用XCode 9,Beta 3. Swift 4.
statsView.createButton("Button name") { [weak self] Void in
//stuff stuff
self?.doSomething()
}
Run Code Online (Sandbox Code Playgroud)
我得到了这样的数百个错误,如何修复它们?
错误:
Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()'
Argument passed to call that takes no arguments
Run Code Online (Sandbox Code Playgroud) 由于苹果已经发布了支持Swift 4.0的Xcode 9 beta版.我一直在尝试从Swift 3.2升级我的代码到Swift 4.0.但它一直给我failed to import bridging header错误,而我的项目成功构建.
Xcode错误细节:
转换为当前Swift语法失败
在尝试迁移之前,请确保使用当前配置的Swift版本成功构建所有选定目标.
在将代码库升级到swift 4时,是否有任何人遇到类似的错误.
Swift 4有Codable,真棒.但UIImage默认情况下不符合它.我们怎么做?
我试过singleValueContainer和unkeyedContainer
extension UIImage: Codable {
// 'required' initializer must be declared directly in class 'UIImage' (not in an extension)
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let data = try container.decode(Data.self)
guard let image = UIImage(data: data) else {
throw MyError.decodingFailed
}
// A non-failable initializer cannot delegate to failable initializer 'init(data:)' written with 'init?'
self.init(data: data)
}
public func encode(to encoder: Encoder) throws {
var …Run Code Online (Sandbox Code Playgroud) 我正在调用AVFoundation的委托方法来处理照片捕获,但是我很难将它生成的AVCapturePhoto转换为具有正确方向的UIImage.虽然下面的例程是成功的,但我总是得到一个面向右侧的UIImage(UIImage.imageOrientation = 3).使用UIImage(数据:图像)并尝试首先使用photo.cgImageRepresentation()时,我无法提供方向?takeRetainedValue()也没有帮助.请协助.
在此处,图像方向至关重要,因为生成的图像将被输入Vision框架工作流程.
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// capture image finished
print("Image captured.")
if let imageData = photo.fileDataRepresentation() {
if let uiImage = UIImage(data: imageData){
// do stuff to UIImage
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新1: 阅读Apple的Photo Capture编程指南(iOS11过时),我确实找到了一件我做错的事情:

更新2 链接到GitHub上的完整项目:https://github.com/agu3rra/Out-Loud
在swift中处理一个项目,我正在尝试启动AVPlayer,由于某种原因,我告诉我一个例外说法
AudioHardware.cpp:1200:AudioObjectRemovePropertyListener:AudioObjectRemovePropertyListener:没有给定ID为0的对象.
我想问题出在我的网址上.这是我的代码
func initPlayer() {
let url:NSURL = NSURL(string:"https://purelight1-163000.appspot.com/api/user/v2/media/track/60/sample")!
self.playerItem = AVPlayerItem(url: url as URL)
self.player=AVPlayer(playerItem: self.playerItem!)
let playerLayer=AVPlayerLayer(player: self.player!)
playerLayer.frame = CGRect(x: 0, y: 0, width: 10, height: 50) // actually this player layer is not visible
self.view.layer.addSublayer(playerLayer)
}
Run Code Online (Sandbox Code Playgroud)