Preferences for opening curly brackets in Xcode - objective-c

I have this problem.
Because i always put "{" in the same line of the function name, like:
- (void)doSomething {
...
}
it's annoying that Xcode creates functions like:
- (id)initWithFrame:(CGRect)frame
{
...
}
So, is there any option to change this behavior of Xcode?

You can go look in the Xcode app bundle (Ctrl-click on it and choose Show Package Contents) and then go to Contents/Developer/Library/Xcode/Templates and find the actual template files.
Then you can change the format of the default code to your hearts content. Remember that upgrading Xcode will probably undo your changes.
Additionally why not post a ticket on bug report.apple.com asking for more consistent template code :)

Related

IntelliJ File and Code Templates error

This isn't specifically code-related, but about the templates of IntelliJ IDEA. In File -> Settings... -> File and Code Templates, it shows the templates of various file types that you can edit (when you create a file of that type, it provides the layout from the template, pretty simple). However, my problem is that it doesn't exactly follow my desired template. For example, let's take the Class type.
This is what I want:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
public class ${NAME}
{
}
This is what I get:
package name
class name {
}
Why does IntelliJ IDEA ignore my moving of the block character onto the next line? How do I fix this? It's incredibly frustrating.
IntelliJ is deciding where to put the brace using your current code style.

How to intercept reading of plist values in Objective-C code?

We're using the new Urban Airship iOS plugin for PhoneGap.
In the plugin's plist file, we're supposed to enter the app-specific keys needed to enable push notifications.
The problem is we have two versions, free and paid, of the same app, but the plist file only accommodates one version.
Essentially, we need to modify the Objective-C code to read different plist values, depending on whether it's the free or premium version.
We currently manage both versions with the same code base and Xcode project. Unless we change the plugin code, it seems like we need to create a new Xcode project, which we don't want to do.
How do we adjust Urban Airship's Objective-C files to read different values from the plsit file?
Sorry to keep you waiting, I wanted to give you a very detailed answer instead of rushing last night :) So here we go.
First in your project we need to add a new target. Go to your project settings and right click your target. Click duplicate.
You'll get a new target probably named Target-copy. You'll also get a new info.plist file just for that target.
Next we're going to edit our Pro version's Built Settings. Scroll or search and find Apple LLVM compiler 4.0 Preprocessing. Add to both your Debug and Release configurations. I normally just go with the simple PRO=1. You also need to add PRO=0 to your lite version or it will be undefined when you try to build that version.
Now lets look at how to add a custom plist like I'm sure you'll need. First create two folders. Its important these are folders not groups. In each folder we can create a plist with the exact same filename.
Since Now you can add something to each of them. I just added a key property and a value pro string / lite string. Finally to the code. In the sample project I made I simple overrode viewDidLoad but obviously this will work anywhere. Since the plists have the same name you can load them with one line of code. They'll never get mixed up because they are only copied to their respective target. If you need to do code level based logic you can use the PRO preprocessor we made.
- (void)viewDidLoad
{
[super viewDidLoad];
// This will load the proper plist automatically.
NSLog(#"Plist Value: %#",[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Property List" ofType:#"plist"]] objectForKey:#"property"]);
// Also remember we set up a preprocessor PRO. you can use it as well.
if (PRO) {
NSLog(#"Only Show for Pro");
} else {
NSLog(#"Only Show for Lite");
}
NSLog(#"This will show for both");
}
This is the method I use for all my lite/pro version apps so I can share a common codebase without copying it between projects or other complicated systems. It has worked pretty well for me so far :) Happy Coding!
Source
Figured someone may be able to use the project to look at so here it is on GitHub.

Localizing the Cut|Copy|Paste menu on iOS

Im having some issues localizing a danish app ive made. (The language, not the pastry)
I have set the CFBundleDevelopmentRegion to da_DK for danish in my info.plist, but the popup appearing for text input is still in english, even on phones running the danish OS.
How in Jobs name can i change this ?
The test device is a non-jailbroken iPhone 4S running iOS 5.1 with Danish as its iOS setting, and a danish itunes account associated.
I do not use .xibs for designs. all interfaces are programmed as viewcontrollers.
In the Xcode's file tree (Project Navigator) select your project. in the right hand pane select your project again. select Info and add your language.
I created a sample project, this is the result:
You can do this directly in the info.plist. Something like this:
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>de</string>
<string>es</string>
<string>ja</string>
</array>
Try adding/setting the "Localized resources can be mixed" flag in Info.plist to YES.
You must localize your app in Danish to make the standard UI elements appear in that language. This is to avoid having a UI with mixed languages.
If you don't use xibs, you'd usually do this by adding a Localizable.strings file to your project. In Xcode's "Add File" dialog, you can use the "Strings File" template (under "Resources") for this.
To actually localize the strings file, open the file inspector (⌘ ⌥ 1) and click the + button in the "Localization" section. You'll end up with the file being displayed as a group in the project navigator, with a sub-entry for each language.
The strings file has the format:
"Label_Text" = "Smørrebrød";
(don't forget the semicolon)
To use localized strings in your code, you can use the NSLocalizedString macro like this:
myLabel.text = NSLocalizedString(#"Label_Text", nil);
(The second parameter is for a comment. This can be useful if you use the genstrings tool to extract localizable strings from your code and give the resulting file to a professional translator.)
If you use the English strings as keys, you can leave the English version of Localizable.strings empty (but don't delete it).
Having a Localizable.strings file in the language that the user has selected will also cause standard UI elements, such as the editing menu, photo picker, and so forth, to appear in that language.
If you can't get it working the official way, as provided by #vikingosegundo, you can do this with some creative engineering (Creative as in, oh my god that is dangerous). I discovered this method when I accidentally overrode [NSBundle localizedStringForKey:value:tableName:].
1) Add a category to NSBundle with the following methods:
#import <objc/runtime.h>
+ (void) load {
Method original, swizzled;
original = class_getInstanceMethod(self, #selector(localizedStringForKey:value:table:));
swizzled = class_getInstanceMethod(self, #selector(swizzled_localizedStringForKey:value:table:));
method_exchangeImplementations(original, swizzled);
}
- (NSString*) swizzled_localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
NSLog(#"Key: %#. Value: %#", key, value);
return [self swizzled_localizedStringForKey: key value:value table:tableName];
}
2) Where I simply log the key/value, you want to put an if ([key isEqualToString: xxx] ) block. In there, you want to catch (at least some of) the following key values: Cut, Copy[Menu], Select, Select All, Paste, Delete[Menu], Replace..., Define, Speak, Pause. These are the default values that can appear there.
3) When you have caught the value you can look up in a custom table or use hardcoded values. If you look up in a custom table make sure you have a catch in your swizzled method to avoid infinite looping in your custom table.
NB: Why do you need to swizzle? Because this over-rides all Apple text for you app. You will still want the defaults for all the other strings, so you need to swizzle to get the defaults for the strings you aren't interested in.
Good luck.
Paul
Search if your .xib is localized (you'll find it in the inspector on the right panel) if so go to your Project/Target-Settings press the +-Sign and select "Duplicate English to Danish" or something which means the same (I can't check the right item at the moment)
Btw it's called iPhone 4S.

xcode - warning there's no getter/setter for property not even mentioned in the code!

I got the warning :
property 'textField' requires method
'-textField' to be defined - use
#synthesize, #dynamic or provide a
method implementation.
Now, there is no such property defined in my project! More bizarre, if I just click save in Interface builder and build again, the build is successful - though, right on the line with '#end' the warning appears. Also weird: if I begin to write some code ..and then delete it just the way it was before writing it (maybe not code..anything) and then build&go the warning with the textField appears again. Could be a bug of sdk? What could be happening?
Go into interface builder, and click File > Read all class files. Save and quit IB. Go back into Xcode and click Build > Build & Clean. Build your project.

Does Xcode support regions?

Does Xcode support anything akin to Visual Studio style #region directives for arbitrary code folding?
No, you can only fold code on various defined scoping levels in Xcode.
You can use little tricks to make navigating via the function menu easier, though.
#pragma mark
Allows you to create a grouping where the label following mark will show up in the function menu. If the label is a hyphen, a separator is inserted into the function menu.
Also, the following labels in comments will show up in the function menu:
// MARK:
// TODO:
// FIXME:
// !!!:
// ???:
Obviously since #pragma mark is not really portable, if you're building a portable application and need it to work with a compiler that doesn't just ignore #pragma directives that it doesn't understand, the comment-style mark is a decent alternative.
I am going to hell for this but here goes:
At the top of a given file, put
#define FOLD 1
Wherever you want to fold something, wrap it in an if block like so:
if(FOLD) {
// your code to hide
// more code
}
That will let you fold it away out of sight.
That won't work in the place you want it most, that is, around groups of functions or methods.
It may be useful inside a long, linear method with no internal conditionals or loops, but such methods aren't common in general Mac OS X UI code, though if you're writing some big numeric or graphics-crunching code it could help group things.
And the if(fold) is entirely superfluous. Just use the braces inside a method or function and Xcode will fold them.
Try this way :
//region title1
{
//region Subtitl1
{
}
//region Subtitl2
{
}
}
It can do like that :
Without support for .Net style regions, being able to collapse all your functions at the same time is the next best thing.
command-option-shift-left arrow
to collapse all.
command-option-shift-right arrow
to expand all.
Xcode will remember the last state of collapsed functions.
A useful option in XCode 12 (maybe before), is an option in preferences "Code Folding Ribbon"
When you check it, the source code looks like this
When you hover the mouse over this ribbon, you get foldable regions based on brackets, like this
When you click the Ribbon, it folds the bracket region, like this
Its not as the regions in Visual Studio, where you can place them wherever you want, but they're good enough to tidy up your code files.
To answer your question...No. And It drives me nuts.
If you have the opportunity/ability you can use AppCode for this. I've been using it for a few years and it usually beats Xcode in many areas.
Also I specifically use AppCode because of these features:
Ability to use regions
Searching classes, text and usages is MUCH faster.
Refactoring is also faster.
Cleaner and more customizable UI.
Tabs are handled (in my opinion) much better than in Xcode.
FOLDING. You can actually change what levels of folding you want. Why Apple thought there should be no quick-key to fold extensions is beyond me. And fold ribbons? Really Apple? Yes they're pretty and all but most professionals use hotkeys for everything.
Better GIT integration.
Support for live updates in SwiftUI
If you use other Jetbrains IDE's like PyCharm or Android Studio the UI is exactly the same.
Some downsides of AppCode:
Some things that work in Xcode aren't supported
Visual #colorLiteral(). When using them they don't show a color picker.
No Storyboard support. Annoying to have to open up Xcode. If you write your UI in code this is a moot point.
Editing .plist files isn't as nice. Doable, but not nice.
Initial indexing can take a while.
Cost. But I would argue the time savings in just navigation will compensate for this.
Kind of a lot for a simple question but I think it's nice having alternatives.
Put your desired code inside brackets { }, and it will become a folding zone.
But you have to keep in mind that brackets also define variables scope, so this code should not have variables declarations which will be used outside these brackets.
One nice solution I just found:
Put your project into one big namespace.
Close and reopen this namespace for the individual sections of your source file:
namespace myproj { // members of class MyClassA
void MyClassA::dosomething()
{
}
void MyClassA::dosomethingelse()
{
}
} // members of class MyClassA
namespace myproj { // members of MyClassB
void MyClassB::dosomething()
{
}
void MyClassB::dosomethingelse()
{
}
} // members of MyClassB