Winter 2015 / Lecture 10 - Broken Twitter Package
Asked Answered
P

4

5

Trying to follow along and code the Smashtag project while watching the Lecture 10 iTunes video.

When I add the dowloaded Twitter package to my Smashtag project, XCode couldn't find the Tweet class when I made reference to it in the TweetTableViewController.

Because of the problem described above, I added the four classes belonging to the Twitter package individually to the project. XCode found the four classes but adding them in this manner generated 11 compile errors.

I'm using XCode Version 6.3 (6D570) which is subsequent to the iOS 8.3 release.

Has anyone else encountered this issue?

Thank you for reading my question. ~ Lee

Phenocryst answered 18/4, 2015 at 14:55 Comment(0)
B
9

Possibly not the most-correct (read: best practice) way to do this, but I'm going to chalk it up to doing what it takes to finish the course.

I just went through the list of compile errors and changed the relevant properties to var instead of let. Constants can't be changed and in the new version of Swift they can only be instantiated once. So for the sake of not rewriting too much code, I chose to make certain properties vars instead of lets.

Other bugs I found following the iTunes U course:

  • The named ‘handler:’ argument needs the name explicitly in a few places.
  • The simulator will show "TwitterRequest: Couldn\'t discover Twitter account type.” until you go to Settings (inside the simulator) and set the Twitter account. At this point I had to reboot the device, as the call is made in the ViewDidLoad, and thus is only called the first time the view loads. (Alternatively, you could close out the app from the app switcher in the simulator and relaunch that way.)

Here is a gist with corrected code that you can use as a Twitter package that will work with the course and has fixes for the aforementioned bugs, minus the Twitter account setting:

https://gist.github.com/mattpetters/ccf87678ccce0c354398

Bottle answered 21/4, 2015 at 20:43 Comment(4)
mpetts, I can't thank you enough for sharing your code. Because of your generosity, I was able to follow along with Lecture 10 to completion. Everything worked!Phenocryst
I and others taking the Winter 2015 version of this iTunes course owe you a debt of gratitude!Phenocryst
Not a problem! Had to work through it myself and ran into that same dilemma. Good luck with your studies!Bottle
Thanks for the hint with setting the Twitter account in the Settings app. Now I get the error: "TwitterRequest: No response from Twitter." It's an access error, in the plist I had to set NSAppTransportSecurity and allow twitter.com and the pictures come from another resource, need to be unlocked separately.Azal
R
3

As Christian R. Jimenez said, "I went to Settings in the Simulated iphone and add my Twitter Account. And everything works perfect." in http://cs193p.m2m.at/cs193p-lecture-10-table-view-winter-2015/. I just added my Twitter Account and tested it, it works!

Raynaraynah answered 13/5, 2015 at 6:5 Comment(0)
V
2

I had similar problems with the Twitter packages using Swift 2.0 and Xcode 7.2

I'm very new to Swift, so there is a good chance the changes I made are not best practices, but the updated files do work: https://gist.github.com/awaxman11/9c48c0b4c622bffb879f.

For the most part I used Xcode's suggested changes. The two larger changes I made were:

  1. In Tweet.swift I updated the the IndexedKeyword struct's init method to use advanceBy() instead of advance()
  2. In TwitterRequest.swift I updated the signature of NSJSONSerialization to conform to the new error handling system
Venable answered 12/1, 2016 at 14:24 Comment(0)
C
0

I've just had a big session fixing the Twitter package files for this same version of Xcode.

It seems that what has broken is that in this version of Swift, constants ('let x...') may only be initialized once, so if a constant is given a value in the declaration ('let x = false'), it may not be changed in the init() function. The Twitter package gives some constants initial values, but then changes the values in the init() function.

My solution to this was to follow the styles suggested in the current version of the Apple Swift language book: declare (many of) the constants as implicitly unwrapped optionals, unconditionally assign a value to them in the init() function (which value may be nil), then test whether any of them are nil, and, if so, return nil from init().

See https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html, click "On This Page" and choose "Failable Initializers"

Also, in TwitterRequest.swift, I needed to add the parameter name 'handler:' in a couple of calls to performTwitterRequest(request, handler: handler).

As an example of constant initialization, in MediaItem.swift:

<< Original Code >>

...
    public let aspectRatio: Double = 0
...
    init?(data: NSDictionary?) {
        var valid = false
        if let urlString = data?.valueForKeyPath(TwitterKey.MediaURL) as? NSString {
            if let url = NSURL(string: urlString) {
                self.url = url
                let h = data?.valueForKeyPath(TwitterKey.Height) as? NSNumber
                let w = data?.valueForKeyPath(TwitterKey.Width) as? NSNumber
                if h != nil && w != nil && h?.doubleValue != 0 {
                    aspectRatio = w!.doubleValue / h!.doubleValue
                    valid = true
                }
            }
        }
        if !valid {
            return nil
        }
    }
...

<< Updated code >>

...
    public let aspectRatio: Double
...
    init?(data: NSDictionary?) {
        if let urlString = data?.valueForKeyPath(TwitterKey.MediaURL) as? NSString {
            if let url = NSURL(string: urlString as String) {
                self.url = url
                let h = data?.valueForKeyPath(TwitterKey.Height) as? NSNumber
                let w = data?.valueForKeyPath(TwitterKey.Width) as? NSNumber
                if h != nil && w != nil && h?.doubleValue != 0 {
                    aspectRatio = w!.doubleValue / h!.doubleValue
                    return
                }
            }
        }
        return nil
    }
...
Councilwoman answered 18/4, 2015 at 18:33 Comment(2)
Bill Evans, thank you for your prompt and comprehensive answer to my question. So far, I've gotten the User and MediaItem classes to compile and am currently working on the Tweet class.Phenocryst
Bill Evans, would you be willing to share your updated Twitter package? It would be greatly appreciated.Phenocryst

© 2022 - 2024 — McMap. All rights reserved.