Swift and Essentials error in IOS 8


#1

Sorry for the Dupes! slow intertubes…

Swift returning Error: Type ‘ViewController’ does not conform to protocol ‘SEssentialsCarouselDataSource’

Implemented #Required as follows but something is askew…

/** Return an item at a given absolute index. Note that the size of the item when displayed in the carousel is determined by the size of the view returned here. The item at the focus point of the carousel will be guaranteed to match exactly the size of the view supplied, views for items off focus may be transformed and appear larger or smaller depending on the carousel type.
    @param carousel The parent carousel
    @param index The index of the requested item in the carousel
    */
    //-(UIView*)carousel:(SEssentialsCarousel*)carousel itemAtIndex:(NSInteger)index;
    func carousel(carousel:SEssentialsCarousel!, itemAtIndex index:Int)->UIView {
        var view:UIView = UIView(frame: CGRect(x:0,y:0,width:40,height:40))
        return view
    }

    /** The total number of items in the carousel (visible and off-screen included).
    @param carousel The parent carousel
    */
    //-(NSUInteger)numberOfItemsInCarousel:(SEssentialsCarousel*)carousel;
    func numberOfItemsInCarousel(carousel:SEssentialsCarousel!)->Uint {
        return 3 as UInt;
    }

#2

The error you are seeing indicates that not all of the methods requird by the protocol have been implemented properly.

It looks like the issue is that the itemAtIndex: method needs to return a “UIView!”, not a “UIView”.


#3

tkelly

The error you are seeing indicates that not all of the methods requird by the protocol have been implemented properly.

It looks like the issue is that the itemAtIndex: method needs to return a “UIView!”, not a “UIView”.

Not that simple–the Swift compiler is not that smart.  So, yes, the error obviously means the #Required’s are not satisfied. And …DataSource has two Required’s nd no Optionals.  Alas, I suspect the problem is not syntactical but related to Type. Looks like I get to dive deeper in Swift and learn to coerce <Types>


#4

Note: I expected code completion to automatically supply the func translation from obj_c to Swift…and it did not.–had to do it myself, by hand.  Suggests the problem may be either xcode bug or a built-in expectation about source pathing that is not being fulfilled.


#5

And the answer is!?

Do Not Cut And Paste (as this will disable code completion).  Let code completion handle the conversion; 

Why?

In the following instance the Shinobi obj_c header returns NSUInteger, but Swift wanted an Int

 obj_c: -(NSUInteger)numberOfItemsInCarousel:(SEssentialsCarousel*)carousel;

Swift: func numberOfItemsInCarousel(carousel: SEssentialsCarousel!) -> Int

So, add the declarations in the Class definition: class ViewController: UIViewController, SEssentialsCarouselDelegate,SEssentialsCarouselDataSource

Then start typing the #Required methods and let code completion suggest the Swift conversion. 


#6

Ah right, good to hear you got to the bottom of it then!

We’re still getting used to Swift too, but its good to hear that the community is interested too!