Home > Software engineering >  Xcode: Quickly open file given hierarchical structure
Xcode: Quickly open file given hierarchical structure

Time:06-07

How can one search methods/structs/classes when one has a struct called, say, P2 and multiple files extending it?

For example

//File1.swift
struct P1 {}

// File2.swift
extension P1 {
   struct P2 {
      func someFuncInP2() {}
   }
}

// File3.swift
struct M1 {}

extension M1 {
   struct P2 {
      func someFuncInP2() {}
   }
}

In this case, let's say I want to search for M1.P2's definition. How can one do that with Xcode's open quickly tool? If I search for "P2 it will show both P2s but I'm only interested in M1's P2. So, if I search for "M1.P2" it finds nothing.

enter image description here

CodePudding user response:

In this case, let's say I want to search for M1.P2's definition. How can one do that with Xcode's open quickly tool? If I search for "P2 it will show both P2s but I'm only interested in M1's P2.

I don't think Open Quickly... is sophisticated enough to understand the relationship between symbols, so using it the way you want isn't going to work. Another example of the same thing is when you're trying to get to a given class's version of a method that's implemented in many classes, like viewDidLoad() As far as I know, there's no way to specify the class that you're interested in along with the method name.

Open Quickly... is in the File menu and does a pretty good job of opening files; there are better tools for finding things within a file. If you know that you're looking for part of a certain class or struct, it's usually enough to open the file for that entity. In your case, instead of searching for M1.P2.someFunc, just search for M1. (That actually won't work for this specific case because the name M1 is too short; the command needs at least 3 characters before it provides results.) If you want the viewDidLoad() in a particular view controller, search for the view controller instead.

The symbol navigator is one tool that might be a better choice for what you're trying to do; you already know that you want something in M1, and the symbol navigator makes it easy to look at the contents of that struct. I'd guess that you might have many types, like M1, M2, M3...M10, all of which might have their own P2 struct, and the symbol navigator is good way to jump between those.

  • Related