CodeSmile
CodeSmile

Reputation: 64477

How to create a global alias for an Objective-C class?

I keep typing NSStrung and NSStrong for NSString. It was so bad today, it made me wish I had a global alias for those so they worked just like NSString.

This made me wonder if this is somehow possible to setup a global alias for a class name (or function) in Xcode? Without writing code that is.

Perhaps there's even some way that I can Xcode to autocorrect NSStrung and NSStrong to NSString, or a tool that does this in any program.

Upvotes: 1

Views: 4571

Answers (5)

andrewchan2022
andrewchan2022

Reputation: 5310

how about typedef.

typedef exist_class new_class;

Upvotes: 0

teriiehina
teriiehina

Reputation: 4761

What you are looking for is certainly:

@compatibility_alias ExistingClass OldClass;

It is pretty straightforward but if you need more info, you can read about it in the GCC manual or this NSHipster article

The answer is pretty late but your question is the first link in DuckDuckGo for the objective-c class alias search terms

Edit: a related question was answered on this SO

Upvotes: 8

Alex Gray
Alex Gray

Reputation: 16473

This is easy / wonderful / I do it all the time (unabashedly to the extreme, even)..

See @7usam's answer for good advice on how to use in your code / disclaimers etc... but go to my project's AtoZMacroDefines.h for more goodness along the lines of..

#define            NSR NSRect
#define           NSPI NSProgressIndicator
#define        NSSPLTV NSSplitView
#define          NSTSK NSTask
#define            NSS NSString
#define           NSIV NSImageView
#define          NSBWM NSBorderlessWindowMask
#define           NSDO NSDragOperation
#define         NSSEGC NSSegmentedControl
#define            NST NSTimer
#define          NSRNG NSRange
#define          NSMIS NSMutableIndexSet
#define         NSTBAR NSToolbar
#define           NSSZ NSSize
#define          NSAPP NSApplication
#define           NSDE NSDirectoryEnumerator
#define          NSVAL NSValue
#define       NSDCLASS NSDictionary.class
#define           NSFH NSFileHandle
#define         NSBUTT NSButton
#define       NSURLRES NSURLResponse
#define            NSV NSView
#define            NSA NSArray

I actually create this file from a plist, which is easy to edit, etc... and then I have a mechanism that generates the header for me. Again, check out the project if this kind of ā“¢š•™iāœ tickles your fancy..

For all the player haters out there... who want to actually use their code with.. gasp.. other people... You can easily move between the "standard" / "long-form" and your "internal" "shortcuts" via git's smudge and clean, without anyone knowing your dirty little secret. Just pre/post-process in/out your desired "replacements" before / after checking out/committing, etc.

Upvotes: -10

7usam
7usam

Reputation: 1009

I just got a question of mine answered and remembered this. Here is my question.

Basically what you can do is have #define NSStrung NSString (and others) in the default Prefix.pch file.

I would suggest to do this in your own custom template, but you can also do it by opening the plist /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/Project Templates/Base/Base.xctemplate/TemplateInfo.plist and adding your #defines (as specified above) in the value of the Definitions > ___PACKAGENAME___-Prefix.pch > Beginning key.

This is definitely not best practice, but it solves your problem. Use at your own risk.

Upvotes: 0

7usam
7usam

Reputation: 1009

I think you should just try to get it right, but as a possible answer to your question, you can set up NSStrong and NSStrung text substitution entries in "Language and Text" System Preferences. In Xcode enable Edit > Format > Substitutions > Text Replacement.

Theoretically that should work, but now that I've tried it on my computer it seems to be inconsistant, and doesn't do the replacement every time. The issue could be something specific to my computer so maybe try that out.

Another option to help you out is add a snippet for NSString with the "shortcut" being NSStrung/NSStrong , that way while you are typing it you could just press enter/return/tab to get the correct NSString. The downside is it won't correct it if you pressed the spacebar which is what you would do normally if you're not looking at the screen.

Upvotes: 3

Related Questions