You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 4/Google Maps崩溃求助:NSInvalidArgumentException异常

Fixing Google Maps Crash in Swift 4: -[UIView setDelegate:]: unrecognized selector

Hey there, let's break down this crash you're hitting—it's a super common gotcha when setting up Google Maps in iOS projects, and I bet we can fix it quickly.

What's Actually Happening?

The error -[UIView setDelegate:]: unrecognized selector sent to instance tells us one clear thing: you're trying to set a delegate on a plain old UIView, but UIView doesn't have a delegate property. Only GMSMapView (the Google Maps view class) does. So somewhere in your setup, a UIView is being treated like a GMSMapView.

Step-by-Step Fixes

Let's go through the most likely culprits:

  • Check your Storyboard/XIB view class
    Open your MapViewController's Storyboard or XIB, find the view you want to use as the map. Then:

    1. Select the view, open the Identity Inspector (right panel, third icon).
    2. In the Class field, make sure it's set to GMSMapView (not the default UIView).
    3. Double-check the Module field—set it to your project's module or choose Inherit from Target to avoid class mismatches.
  • Fix your code's view declaration
    Head to your MapViewController code and look for the IBOutlet or code-created map view. If it's declared as UIView, that's the problem:

    // ❌ Wrong: This is a plain UIView, no delegate property
    @IBOutlet weak var mapView: UIView!
    
    // ✅ Correct: Declare it as GMSMapView
    @IBOutlet weak var mapView: GMSMapView!
    

    If you're creating the map programmatically, make sure you're initializing GMSMapView, not UIView:

    // ❌ Wrong
    let mapView = UIView(frame: view.bounds)
    
    // ✅ Correct
    let mapView = GMSMapView(frame: view.bounds)
    
  • Verify your delegate setup timing
    Make sure you're setting the delegate after the map view is fully loaded. The safest place is viewDidLoad():

    override func viewDidLoad() {
        super.viewDidLoad()
        // Only set delegate once the map view exists
        mapView.delegate = self
        // Add your other map config here (camera position, markers, etc.)
    }
    
  • Double-check IBOutlet connections
    Sometimes IBOutlets get connected to the wrong view (since you have three UIViews in your controller). Right-click the mapView IBOutlet in your code—you'll see which view it's linked to. Confirm it's the one you intended to use as the Google Map.

Bonus: Clean Project Cache

If none of the above works, try cleaning your Xcode project (press Cmd + Shift + K) and rebuilding. Xcode's cache can sometimes hold onto old class references that cause weird issues like this.


内容的提问来源于stack exchange,提问作者Bruce Bookman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:03:50