Why does Xcode autocomplete fail to work when I alloc/init a variable? - xcode

I have a ridiculous problem with Xcode 4.3.2. Whenever I declare a new variable say
NSMutableDictionary *var = [NSMutableDictionary alloc] init];, Xcode autocompletes NSMutableArray in LHS of the expression but not in RHS.
This is irritating obviously. Often causes typos which I have to re-correct going back and certainly hampers the flow. From my experience it doesn't happen in any other situation.
Anyone else ever faced this? Should I raise a bug?

I think that's because Xcode doesn't know if you want to make some operation with the class and tries to find an appropriate variable. So if you want Xcode to autocomplete your class name in RHS, first write [] then start typing inside: [NS...]

Related

Xcode turn off code completion for NSObject

Is there a way to modify Xcode's (6.4/7.x beta whatever) code completion in such a way that neither the methods nor the properties of NSObject in a subclass of it are shown? I think, I don't use them frequently and it is pretty annoying when searching for another function whose name I can only slightly guess and then have to scroll through all the unnessecary suggestions coming from NSObject.
Thanks for your help
Not sure that its possible to adjust autocomplete only for NSObject and descendants (of course it might be the way), but there is another nice tool I'm using:
https://github.com/FuzzyAutocomplete/FuzzyAutocompletePlugin
It won't solve a problem of unnecessary suggestions you see, but
it's easier to add and remove from Xcode (comparing to manually internal Xcode files edit)
it solves a problem of "searching for another function whose name
I can only slightly guess"

NSTextView setNeedsDisplay not working under Mavericks?

My MacOS Cocoa application displays a window of static text, meaning it should not be changed by the user, should not be first responder, etcetera. The only thing that happens to the text is that each word of it changes color (from "idleColor" to "highlightColor", and then back again) at a specific point in time. It is similar to a Karaoke display - individual words change color, and then change back, under program control, based on a list of timed events.
All of this works beautifully under MacOS 10.7 and 10.8. BUT, under 10.9, the text color does NOT change UNLESS I click in the window and continually move the cursor around, so I am manually highlighting (and un-highlighting) some of the text, continuously. If I do this, the regular words behave as intended. Essentially, it feels like the OS is refusing to update the window under program control, unless I am forcing it to update by manually performing something that requires the UI to respond.
The code that performs the color changes is as follows:
if (sEvent.attribute == HIGHLIGHT_ON) {
[sTextView setTextColor:highlightColor range: currentRange];
textIsLitUp = YES;
}
else {
[sTextView setTextColor:idleColor range: currentRange];
textIsLitUp = NO;
}
[sTextView setNeedsDisplay:YES];
(sTextView is a subclass of NSTextView.)
Now, if I comment out that last line, then I get the same, incorrect behavior under 10.7 and 10.8. In other words, under 10.9, the setNeedsDisplay method is not working, or not working the same way.
Does anyone have any ideas about working around this, or have any other light to shed on the problem? Or am I doing something terribly wrong? It is CRITICAL to the application that the changes to the textColor happen without latency!
EDITING MY QUESTION - to answer it:
Found the answer elsewhere here! I needed to call setNeedsDisplay on the main thread - it was in a secondary thread. The weird thing is that it always seemed to work fine under 10.7 and 10.8. It only broke under 10.9. So I just changed this:
[myTextField setNeedsDisplay:YES];
To this:
dispatch_async(dispatch_get_main_queue(), ^{[myTextField setNeedsDisplay:YES];});
…and it seem to have worked. Hope this helps someone else…
You don’t want to do any of the changing of AppKit objects in non-main threads—it’ll work sometimes, maybe even often, but then every once in a while it’ll crash, and you’ll wonder why. So:
[sTextView setTextColor:idleColor range: currentRange];
needs to be on the main thread, too.

App Crashing on Simulator and Device but not when Profiling

The app crashing with signal SIGABRT (the debugger output is child already added. It can't be added again) in the simulator and on the device. Runs fine when I profile the app in Xcode while running it on the simulator or the device. Why is this?
Update: I've figured out that this line of code is causing the problem:
Mover *moverObject = [[[Mover alloc] init] autorelease];
NSMutableArray * array = [moverObject moveToward:startPoint :finalPoint]//<-- This is the problem
moveToward is method that returns a NSMutableArray containing the points from the startPoint to the finalPoint. This worked fine early today but after I started testing something new I guess I broke it. I made no changes in the actual Mover.h/.m just in the GameLayer.m (where I was adding code). I'm not sure what I added to cause the problem.
Update 2: I did some more digging using breakpoints and I found that
GameLayer *gameLayerObject = [[GameLayer alloc] init];<-- This causes the crash
Inside mover.m where the method moveToward:: is, this is the furthest it will go without crashing. Again the error is child already added. It can't be added again. Why does this happen?
This may just be coincidence. Hard to say because you didn't post any code.
The error message is clear though: you're trying to addChild the same node more than once, either to the same parent or to a different parent. Check your code for situations where this can occur.

Weird Xcode behavior

Lately something weird has been happening to my projects in xcode: I've been trying to learn a lot of new stuff, and doing so by testing things out in different simple cocoa apps (written by me, from scratch). sometimes I will get a code that doesn't have any error messages, but when i run it, i will stop at some break-point. I then conclude that I have probably done something wrong, and restores the code back to the form it was before the error, but from then on out it is impossible to get the code to run. even if i restore the code to a state that i am 100 % sure that has worked before, it just stops at the same break-point. in order to fix this problem, i have to copy my code from the class this has happened to, delete the class, make a new one with the exact same name, paste the exact same code back in the class, and voila, it works again. what on earth is happening? my newest problem code goes like this:
-(IBAction)openFile:(id)sender {
NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
NSURL *fileURL;
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setAllowedFileTypes:[NSArray arrayWithObject:#"txt"]];
if ( [openPanel runModal] == NSOKButton ) {
fileURL = [openPanel URL];
}
[openPanel release];
}
I know this code has worked before. It is currently my only method, and it activates when i press open in the menu. If I delete everything inside the method, so that pressing open should do nothing it stops at a break-point inside the method anyway. I have had exactly the same kind of problem before with openGL codes, and with a method that used c syntax to do file reading. Does anybody know what kind of horrible mistake I'm making over and over again?
A Breakpoint is something you yourself set explicitly to tell Xcode to pause the program at this exact line. Superficially it might look like the program crashed, but in reality it's just waiting for you to tell it to go on.
This page looks like it has a nice explanation of the breakpoint interface in Xcode. (This is from a framework called Cocos2D, but ignore that. You should stick to ordinary Cocoa until you know what you're doing.)

Taming XCode’s auto-complete options

I am fairly new to XCode and the Objective-C language.
When I am instantiating a class, for example an NSMutableArray, XCode will provide a whole lot of auto-complete options. Even for an empty class which simply extends an NSObject has many options, most of which seem completely useless.
What is the reason for having so many auto-complete options, or can they be "tamed" in the preferences?
EDIT: this is unintentionally a duplicate.
The auto-complete makes coding way faster. For example, in [[NSMutableString alloc] initWithCapacity:5], I only had to type the characters that are in bold face, which is ~ 10/38 = ~26%, so it saves a lot of typing.
That said, it is sometimes unwelcome. You can adjust in "Code Sense" in the Xcode preferences (type COMMAND+COMMA while Xcode is open and in the foreground). You can set the delay for suggestions and also whether it should remember functions you've previously used in that project. You can also disable it completely, if you want to. I don't think you can limit the number of functions it considers, though.

Resources