Xode 4.5 Hello World example doesn't work - macos

I am pretty much an XCode beginner, and I therefore tried the, "Hello World" example given in the XCode documentation: Quick Start Guide: Tutorial: Using Xcode to Write “Hello, World!” for OS X.
I am using XCode 4.5.1 from whose documentation I took this example using OS 10.8.2. I followed the instructions; the example didn't work. It produced the window but not the "hello world" printing. The relevant code is:
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
NSString *hello = #"Hello, World!";
NSPoint point =NSMakePoint(15, 75);
NSMutableDictionary *font_attributes = [[NSMutableDictionary alloc] init];
NSFont *font = [NSFont fontWithName:#"Futura-MediumItalic" size:42];
[font_attributes setObject:font forKey:NSFontAttributeName];
[hello drawAtPoint:point withAttributes:font_attributes];
[font_attributes release];
}
There are two notable things. The last line of this code gives an error:
release is unavailable: not available in automatic reference counting mode
So I commented this line out and ran the program. The window appeared, but no "Hello World". A long message appeared in the All Output, part of which read:
"/Users/me/Library/ScriptingAdditions/YouHelper.osax/Contents/MacOS/YouHelper: no matching architecture in universal wrapper
Hello: OpenScripting.framework - scripting addition "/Users/me/Library/ScriptingAdditions/YouHelper.osax" declares no loadable handlers."
Am I doing something dumb, or am I using the wrong example?

You have to remove [font_attributes release]; at the end of your code.
release, retain, alloc and dealloc are not available in ARC(Automatic Reference Counting). This is what the error says.
And for the second error you should remove anything that is referring to AppleScript and similar scripting addons for the app.

Without seeing you project it's difficult to find out what's wrong with it. My guess is: You forgot to specify the custom view's class in IB (Step 12 in "Design the User Interface").

Related

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.

How to debug EXC_BAD_ACCESS error using Profile in Xcode

After I received a exc_bad_access error I read this tutorial: http://www.ioslearner.com/debugging-exc_bad_access-error-xcode-instruments/ and followed the steps:
Run Profile
Choose Zombies
Everything looks the same on my computer except that I do not get the Zomie Signal.
In order to make sure I did not forget a step I tried it with the example code provided in the Tutorial. There I see the Zombi Signal
So how do I get the zombie signal in my project using Xcode -> Profile?
This is the sample code I use:
I am well aware that the two releases cause the error. Again the question is how get the "Zombie Message" in the Profile using Xcode:
NSArray *currentRestaurant = [restaurants objectAtIndex:i];
RestauInfo *restauInfo = [NSEntityDescription
insertNewObjectForEntityForName:#"RestauInfo"
inManagedObjectContext:context];
[restauInfo release];
restauInfo.Name = [currentRestaurant objectAtIndex:0];
restauInfo.Cuisine = [currentRestaurant objectAtIndex:1];
NSError *error;
if (![context save:&error]) {
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
[restauInfo release];// - this release would cause an EXC_BAD_ACCESS
Another question is: Why do I get bad_access at the second release - and not when accessing restauInfo after the first release?
I found another way detecting faults in memory management:
In Xcode you can choose "Run", "Test", "Profile" and "Analyze"
(By pressing and holding the run Button in the upper left corner)
Running the analyze reveals memory management errors - such as
over releasing
leaks
You are overreleasing restauInfo, its an autoreleased object and you call -release manually twice, causing your crashes.
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSEntityDescription_Class/NSEntityDescription.html

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

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...]

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.)

Three20: unreconized selector when adding TTActivityLabel like in the sample

I add an TTActivityLabel, pretty much like in the sample:
TTActivityLabel *label = [[[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleBlackBezel] autorelease];
label.text = #"Hello World.";
[label sizeToFit];
label.frame = CGRectMake(0, 0, myView.width, label.height);
[myView addSubview:label];
The code crashes on -sizeToFit with:
2010-02-09 14:42:21.065 MIB[17092:207] *** -[UICFFont ttLineHeight]: unrecognized selector sent to instance 0xf15e30
I already figured out, that ttLineHeight is an addition to UIFont, that seems to not be correctly identified by Xcode. How can this happen? Maybe i do something wrong when importing Three20?
When i run the debugger, it interrupts inside the code of TTActivityLabel where the error happens. I am not sure if this is the correct behaviour. Maybe it should not break inside TT code, since this is a static library?
Any help is appreciated =)
Alright, i found the solution.
There was indeed an error made by me when i added the three20 libraries to my project.
"Other linker flags" in build settings was not set to "-all_load -ObjC". I fixed that and now it works fine.

Resources