Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Gary Marston
Gary Marston
7,536 Points

Passing instance variables to other classes

Can someone please help me understand why testdata.mutableArray1 contains nothing () although testate.mutableArray contains data? ( billy,bob). I've been racking my brain for a week over this now. Is it something to do with the NSURLSessionDelegate?

My Code is below:

// // ViewController.swift // test // // Created by sdfsdfsfsdf on 11/05/2015. // Copyright (c) 2015 GM. All rights reserved. //

import Foundation import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    func retrieveData(url1: String) {

        var testData = webConnect()
        testData.callArray()
        testData.downloadData(url1)

        println(testData.mutableArray1)
        println (testData.mutableArray)

    }

    retrieveData("$SOMEURL$")
}

}

class webConnect: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate ,NSURLSessionDataDelegate

{ var mainSession : NSURLSession = NSURLSession()

var mutableArray: NSMutableArray = []
var mutableArray1: NSMutableArray = []

override init()
{
    super.init()
    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    mainSession = NSURLSession(configuration: configuration,
        delegate: self,
        delegateQueue:NSOperationQueue.mainQueue())
}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDataTask, didFinishDownloadingToURL location: NSURL)   {
    var locateData: NSData

    locateData = NSData(contentsOfURL: location)!
    var testArray: NSMutableArray = ["wer","werwer"]
    testArray = ["blah", "blah"]
    self.mutableArray1 = testArray
}

func downloadData(URLString: String)     {
    var request = NSMutableURLRequest(URL: NSURL(string: URLString)!)
    request.HTTPMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")


   var task = self.mainSession.downloadTaskWithRequest(request)

    task.resume()
}

func setArray() {
    var result: NSMutableArray = ["bob"]

    result = ["billy", "bob"]
    self.mutableArray = result
}

func callArray()     {

    setArray()
}

}

1 Answer

Michael Liendo
Michael Liendo
15,326 Points

hey Gary Marston! I see that var mutableArray: NSMutableArray = [] is definitely set in setArray() I'm sure you've gotten that. However I don't see where func URLSession(session: NSURLSession, downloadTask: NSURLSessionDataTask, didFinishDownloadingToURL location: NSURL) is called to set mutableArray1. In addition, on the line request.HTTPMethod = "POST" did you mean to have "POST"as your key and not "GET"? Hope I was able to provide some insight, please let me know if I can further help!

Gary Marston
Gary Marston
7,536 Points

Hi Michael,

Thanks for your reply. I'm expecting self.mutableArray1 = testArray to set the mutableArray1. I'll look at the httpMethod closer when I have the workings correct.

The method in setArray and URLSession i.e self.mutableArray = anArray should suffice I would expect. I'm wondering whether this is due to the delegate not allowing the object to be used..

Gary