从swift 3升级到swift 4时出现错误。使用UICollection performBatchUpdates()方法时出现错误。我的代码看起来像这样,
Class A: UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
func someMethod() {
collectionView?.performBatchUpdates({ [weak self] _ in // error: Expression type '(_) -> _' is ambiguous without more context
self?.collectionView?.deleteItems(at: [IndexPath(item: 0, section: 0)])
}, completion: nil)
})
}
}
答案 0 :(得分:1)
_ in
的意思是:
如果未从performBatchUpdates
传递变量,则
确认变得不必要和不正确。因此,您必须删除_
。但仍要保留in
,因为您需要在 in 中传递对weak
的{{1}}引用,以避免出现内存问题。
答案 1 :(得分:0)
删除“ _”
export class GithubFollowersService extends DataService {
constructor(http:Http) {
super('https://api.github.com/users/IDBRAHIMDEV/followers',http)
}
做到
constructor(private url : string ,private http : Http) { }
getAll(){
return this.http.get(this.url).
pipe(
catchError(
this.handleError
)
)}
答案 2 :(得分:0)
您的整个代码将无法编译,因为该类应该具有NSObject
的后代,此外,performBatchUpdates
的补全中没有变量,因此应删除此_
class A : UIViewController,UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
func someMethod() {
collectionView?.performBatchUpdates({ [weak self] in
self?.collectionView?.deleteItems(at: [IndexPath(item: 0, section: 0)])
})
}
}