Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming an outlet in Swift

Tags:

xcode

ios

swift

I accidentally misspelled one of the outlet's in my view controller and ran into a few issues. When I manually try to correct the typo I get stopped at runtime within AppDelegate I'm shown the message,

Thread 1: signal SIGABRT

which highlights the beginning of the code block:

class AppDelegate: UIResponder, UIApplicationDelegate {
  ...
}

I've found that to fix this issue in Objective-C you would right-click the outlet's original name and "Refactor => Rename" but unfortunately I get the message:

Xcode can only refactor C and Objective-C code

View Controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var billTextField: UITextField!

    @IBOutlet weak var tipRateSegmentedControl: UISegmentedControl!

    @IBOutlet weak var tiLabel: UILabel! // Variable name should be "tipLabel"

    @IBAction func calculateTapped(sender: AnyObject) {
        var userInput = billTextField.text as NSString
        var totalBill: Float = userInput.floatValue
        var index: Int = tipRateSegmentedControl.selectedSegmentIndex
        var tipRate: Float = 0.15

        if index == 0 {
            tipRate == 0.15
        } else if index == 1 {
            tipRate = 0.20
        } else {
            tipRate = 0.25
        }

        var tip: Float = totalBill * tipRate

        tipLabel.text = "$\(tip)"
    }
}
like image 316
Carl Edwards Avatar asked Jan 26 '15 13:01

Carl Edwards


People also ask

How do you rename a class in Swift?

Highlight the code that you want to rename. right click and choose refactor -> rename .

What is IBOutlet and IBAction in Swift?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.


2 Answers

EDIT: Since Xcode9, there's a refactor function; the new good method is the one in this answer


Since Xcode 6, you can look for your outlet names directly in the Find navigator (Cmd+3), it shows occurrences in your code and in your xibs and storyboards. It also works with actions.

Just search the name in the Find navigator.

enter image description here

You can see that the second result is a reference in a storyboard. You can replace one by one by clicking on each result and press Replace, or you can directly Replace All if you are sure you don't break anything.

like image 110
Crazyrems Avatar answered Oct 01 '22 07:10

Crazyrems


Refactor > Rename...

Xcode 9 had many editor improvements including a feature that changes an outlet/action name in the code and storyboard from one place.

In the example Swift code below, right click on btnRequestCode, from the popup menu select "Refactor > Rename...", change the outlet/action name and Xcode changes it in the Storyboard also.

@IBOutlet weak var btnRequestCode: UIButton!

enter image description here

like image 34
Mobile Dan Avatar answered Oct 01 '22 06:10

Mobile Dan