0

I have the following Swift class:

class VideoFaceDetectionController: UIViewController, IPVideoEmbedderControlDelegate {

var videoPlayer: IPVideoEmbedderControl?  // Note, not a weak reference
...

Then I have a method in Objective-C (in a referenced static library): generatePlayer(), as follows:

+ (IPVideoEmbedderControl*)generatePlayer
{
    IPVideoEmbedderControl* control = [IPLoad
        objectWithClass:[IPVideoEmbedderControl class]
        fromBundle:[NSBundle bundleWithIdentifier:videoFrameworkBundleID]];

    control.provider = [IPVideoPlaybackFactory getProvider];

    [control createEmbeddingView];
    return control;
}

I call it in Swift 4 like so:

videoPlayer? = IPVideoEmbedderControl.generatePlayer()
videoPlayer?.translatesAutoresizingMaskIntoConstraints = false;

Now I put a breakpoint on the second line (translatesAuto....), and inspect videoPlayer. It's NIL.

Now I do po IPVideoEmbedderControl.generatePlayer() in the console, and that produces a valid instance, that even has a frame:

 (lldb) po IPVideoEmbedderControl.generatePlayer()
▿ Optional<IPVideoEmbedderControl>

(lldb) po IPVideoEmbedderControl.generatePlayer().description
"<IPVideoEmbedderControl: 0x7faf88c19390; baseClass = UIControl; frame = (0 0; 320 349); autoresize = W+H; layer = <CALayer: 0x60c0002265c0>>"

So how can it be that in the debugger, a valid object is produced and returned, but in the code, a NIL value is returned from the same exact method?

I'm using Xcode Beta 9.0 (Release 5), with an iPad Air 2 (iOS 11) simulator. I can't verify on physical devices right now, because I don't have any running iOS 11 yet.

FranticRock
  • 3,233
  • 1
  • 31
  • 56

1 Answers1

1

If this line is not a typo and you really use it in your app, it does not work as you expect:

videoPlayer? = IPVideoEmbedderControl.generatePlayer()

Try changing the line to:

videoPlayer = IPVideoEmbedderControl.generatePlayer()

You know the next line is a sort of Optional Chaining.

videoPlayer?.translatesAutoresizingMaskIntoConstraints = false;

When videoPlayer is nil, the rest of the line is safely ignored and it does nothing.

All the same in the first line:

videoPlayer? = IPVideoEmbedderControl.generatePlayer()

When videoPlayer is nil, the rest of the line is safely ignored and it does nothing.

OOPer
  • 47,149
  • 6
  • 107
  • 142
  • That was it thank you. To think that you do so much assignment in your code, you can just put a question mark anywhere, and you get silent no-op without any warning. I think they should prevent this. They are trying to be more type safe and explicit, but this is the equivalent of silent no-op on nil instances in Objective-C. Newbie mistake. Thanks. – FranticRock Aug 18 '17 at 19:39
  • @Alex, I agree that there should be some warnings. I don't think you are the only one who make a mistake like this. (Guess why I could write this answer so soon...) You can write a [bug report to swift.org](https://bugs.swift.org/). – OOPer Aug 18 '17 at 19:44