Ambiguous Use of Registerclass with Swift

ambiguous use of registerclass with Swift

Your call to registerClass has a parameter name (cellClass) for the first parameter. In Swift, the first parameter name is omitted.

tableView.registerClass(CustomTableViewCell.self, 
forCellReuseIdentifier: "CustomCell")

The reason the error says ambiguous use of registerClass is because the compiler can't tell which one you are trying to call. There are two registerClass methods on UITableView:

func registerClass(cellClass: AnyClass, forCellReuseIdentifier identifier: String)
func registerClass(aClass: AnyClass, forHeaderFooterViewReuseIdentifier identifier: String)

Your call doesn't match either of these, therefore it's ambiguous.

CoreData/TableView - Ambiguous use of 'subscript'

You have this declaration:

var eventItems: AnyObject?

Then later on you do this:

let eventItem = eventItems! [indexPath.row] 

The problem is that you declared eventItems as AnyObject?, and AnyObject does not accept subscripts. It can literally be any object at all, and not every class works with subscripts, so Swift doesn't know what to do.

Since you're assigning eventItems as the result of a fetch request, it should be declared as either an NSArray? or as something like [Event]?. Also, using that ! after eventItems puts you at serious risk of a crash; don't ever use ! unless you're absolutely sure that you won't accidentally have a nil value.

Converting Objective-C to Swift: Assigning to self

When initializing an object in Swift it is guaranteed to be safe.

Therefore, there is no need to check it for nil values.

UICollectionView items not showing after migration to Swift 2

So it looks like ios9 has a bug when rendering custom cells.

I would suggest creating your custom cells in xibs and then loading them from there.

Make sure to include this in viewDidLoad:

self.collectionView.registerNib(UINib(nibName: "SongCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "SongCell")

self.collectionView.registerNib(UINib(nibName: "SongHeader", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: "SongHeader")

UITableView with variable cell height: Working in IB but not programmatically

The error is pretty trivial:

Instead of

self.addSubview(myCellLabel)

use

self.contentView.addSubview(myCellLabel)

Also, I would replace

// pin top
NSLayoutConstraint(...).active = true
// pin bottom
NSLayoutConstraint(...).active = true
// center horizontal
NSLayoutConstraint(...).active = true

with

let topConstraint = NSLayoutConstraint(...)
let bottomConstraint = NSLayoutConstraint(...)
let centerConstraint = NSLayoutConstraint(...)

self.contentView.addConstraints([topConstraint, bottomConstraint, centerConstraint])

which is more explicit (you have to specify the constraint owner) and thus safer.

The problem is that when calling active = true on a constraint, the layout system has to decide to which view it should add the constraints. In your case, because the first common ancestor of contentView and myCellLabel is your UITableViewCell, they were added to your UITableViewCell, so they were not actually constraining the contentView (constraints were between siblings not between superview-subview).

Your code actually triggered a console warning:

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

Which made me to look immediately at the way the constraints are created for your label.



Related Topics



Leave a reply



Submit