Swift学习求助:Type 'FaceView?' has no member 'eyesOpen'等错误排查
Hey there! I’ve helped a ton of CS193P newbies work through this exact issue—let’s get your face view up and running without those frustrating errors.
First, let’s break down why you’re seeing Type 'FaceView?' has no member 'eyesOpen': that question mark after FaceView means your variable is an optional—Swift isn’t 100% sure it’s actually pointing to a real FaceView object yet. You need to "unwrap" it to access its properties like eyesOpen or mouthCurvature. Here are the most common fixes, ordered by how likely they are to solve your problem:
1. Fix the Optional Wrapping
This is the #1 culprit for this error. Since your faceView variable is optional (FaceView?), you can’t directly access its properties. Try one of these safe approaches:
Option A: Optional Binding (Recommended)
This checks if faceView exists before accessing it—no crashes if it’s nil:
if let unwrappedFaceView = faceView { unwrappedFaceView.eyesOpen = true unwrappedFaceView.mouthCurvature = 1.0 // Positive = smile, negative = frown }
Option B: Implicitly Unwrapped Optional (For IBOutlets)
If you’re using an IBOutlet (connected from Storyboard/XIB), you can change the variable definition to use an exclamation mark instead of a question mark. This tells Swift "I promise this will exist when I use it":
@IBOutlet weak var faceView: FaceView! // Changed from FaceView?
Then you can access properties directly:
faceView.eyesOpen = false faceView.mouthCurvature = -0.5
Option C: Force Unwrap (Use With Caution)
Only do this if you’re absolutely certain faceView isn’t nil (it can crash your app if it is):
faceView!.eyesOpen = true
2. Check Your IBOutlet Connection
Even if you copied the demo code, sometimes the Storyboard/XIB connection gets messed up:
- Open your Storyboard or XIB file.
- Find the FaceView UI element.
- Right-click the element and look at its connections—make sure the
faceViewoutlet is connected to the correct variable in your ViewController. - If there’s a yellow warning or no connection at all, re-drag the outlet from the element to your ViewController code.
3. Verify the FaceView Custom Class
Double-check that your UI element is actually set to use your FaceView class:
- Select the FaceView element in Storyboard/XIB.
- Open the Identity Inspector (the third icon in the right-hand panel, looks like a circle with an i).
- Under "Custom Class", make sure the Class field is exactly
FaceView(Swift is case-sensitive!) and the Module is set to your project name (not "None").
4. Double-Check Property Names
It’s easy to make typos! Open your FaceView.swift file and confirm the properties are spelled exactly as you’re using them:
// Make sure these exist in FaceView.swift var eyesOpen: Bool = true var mouthCurvature: Double = 0.0 // Neutral
Even a tiny typo like eyesopen instead of eyesOpen will cause this error.
内容的提问来源于stack exchange,提问作者Tony Hudson

