简体   繁体   中英

Objective-C & Swift 2.0 and “paranoic” Class

On a Project i use Objective-c 2.0-Swift2.0 and XCode 7.0.

So the matter is: I'm tryin to put some string gotten from txt file in order to be shown into a UiTextView object. The catch from another Object-C call was fine. The reading of the file txt Was fine

...but the putting String into the UITextView! returns me always Nil. It's a dummy work but... it's still not work!

I tried to do the same as single Application Swift File: it works! ps Notice i did a IBoutlet to connect the UIView Object into the StoryBoard to this Class.

Here's the code, if you need more details i would be happy to provide this.

import UIKit

@objc(TextControllerSwift) class TextControllerSwift: UIViewController {
var textRoom: String?
var textPlaying: String?

@IBOutlet weak var textMuseum: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    realplay()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func playText(textSelect: String) {
//textSelect REturn a string value from another Object-C Class -See comments in the Segue add in.

    textPlaying = textSelect
 }

 func realPlay(){

    let path = NSBundle.mainBundle().pathForResource(textPlaying, ofType:"txt")

    if (path != nil){
        //read the file
        do {
            let textRoom = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
            print(textRoom)
        }
        catch {/* let's ignore when i didn't find file */}

        textMuseum!.text="pippo"  //Nil 
       print(textMuseum!.text)    //Nil 

  textMuseum.text=textMuseum.text.stringByAppendingString(String(textRoom))  //Nil 
    }
}}

it give me " fatal error: unexpectedly found nil while unwrapping an Optional value "

Why? Why? in the same project i call another Swift Class and it works really fine. (AKA i followed the guideline for coexisting Swift&Objective-C classes) Thanks a lot in advance for your answers.

EDIT: Added info about the Caller from Objective-C, maybe this helps.

   `if ([segue.identifier isEqualToString:@"segueIntroTesto"]) {

    NSLog(@"%@ in segueIntroTesto" , selectedIntroFilm);
    TextControllerSwift *destViewController = segue.destinationViewController;
    [destViewController playText:selectedIntroFilm];
    }`

EDIT2: : removed *self.viewDidLoad()* under playText func.

EDIT3: : splitted the function playText() into another Function to give time to XCode (uffff) to get the new View then i REMOVED the old damned Outlet :D ...cheers I solved Thanks

Your problem is that you're trying to set the value of an outlet before the view gets loaded. Preparing for a segue happens before outlets are filled in.

Pass selectedIntroFilm into the controller and save it as a property during the segue, then call playText later...during viewDidLoad or viewWillAppear or whatever makes sense for the app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM