Nested functions are disabled; use f-nested functions to re-enable - objective-c

I am just learning Objective C and I am having great difficulty. This is what is typed and it is giving me an error. I typed the text that is bold. What is wrong with it. It gives me the nested function error right after int main(void)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [NSAutoreleasePool alloc] init];
// **#include <stdio.h>
int main(void)
int amount = 1000000;
printf("The amount in your account is $%i\n", amount);
return 0;
}**
NSLog(#"Hello, World!");
[pool drain];
return 0;
}

Your problem is that C and it's brethren do not like functions within functions (putting aside gcc extensions for now).
What you seem to be trying to do is to declare a whole new main inside your main. That's a big no-no. What I suspect is that you've cut-and-pasted an entire C program into the middle of your existing main.
Start with:
#import <Foundation/Foundation.h>
#include <stdio.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [NSAutoreleasePool alloc] init];
int amount = 1000000;
printf("The amount in your account is $%i\n", amount);
NSLog(#"Hello, World!");
[pool drain];
return 0;
}
and work your way up from there.

Related

Adding Blocks to Inactive Dispatch Queue

I have a situation where it would be helpful to be able to add blocks to an inactive queue which is then activated after another event happens. However, the entire thread from which I call dispatch_sync gets locked if the dispatch queue is suspended/inactive.
Is something like this possible? If not what other options do I have?
An example (not my actual use case):
#include <stdio.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[]) {
dispatch_queue_t my_queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL_INACTIVE);
void (^b)(int) = ^void(int c) {
printf("%i\n", c);
};
for(int i = 1; i <= 1000; i++) {
dispatch_sync(my_queue, ^{ b(i); });
}
dispatch_activate(my_queue);
return 0;
}
I figured it out. Using dispatch_async fixed the problem.
Working solution:
#include <stdio.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[]) {
dispatch_queue_t my_queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL_INACTIVE);
void (^b)(int) = ^void(int c) {
printf("%i\n", c);
};
for(int i = 1; i <= 1000; i++) {
dispatch_async(my_queue, ^{ b(i); });
}
dispatch_activate(my_queue);
return 0;
}

Using NSUInteger Input to insertObject:___ atIndex:___

I'm trying to create a simple commandline tic-tac-toe game using an NSMutableArray.
Created a class called "Board" with the method "getPosition"
(I'm assuming this is the best way to get a user input)
I'm asking for position, then casting from int to NSUInteger)
#import "Board.h"
#implementation Board
-(void)getPosition;
{
int enteredPosition;
scanf("%i", &enteredPosition);
NSUInteger nsEnteredPosition = (NSUInteger ) enteredPosition;
NSLog(#"Position = %lu", (unsigned long)nsEnteredPosition);
}
#import <Foundation/Foundation.h>
#import "Board.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSString *currentPlayer;
NSMutableArray *gameBoard=[[NSMutableArray alloc] initWithCapacity:9];
for(int i; i<=2; i++)
{
if(i %2)
{
currentPlayer=#"X";
}
else
{
currentPlayer=#"O";
}
NSLog(#"Player %#, select an open spot 1 - 9 on the board", currentPlayer);
Board *currentPosition = [[Board alloc] init];
[currentPosition getPosition];
[gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem
}
As I understand it atIndex requires an NSUInteger parameter, but I'm receiving the error message:
"Incompatible pointer to integer conversion sending 'Board *_strong"
to parameter of type 'NSUInteger' (aka 'unassigned long')
You're using currentPosition as your index which is a Board object. Perhaps [currentPosition getPosition] is supposed to return an NSUInteger. If so, try rewriting the last portion of your code like this:
Board *theBoard = [[Board alloc] init];
NSUInteger currentPosition = [theBoard getPosition];
[gameBoard insertObject:currentPlayer atIndex:currentPosition]; //this is where i have a problem

Objective-C: printing enum values

I was trying to print out the enum constant in Objective-C on Xcode.
The code:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
enum boolean{
no, yes
};
NSLog(#"%d", yes);
}
return 0;
}
I ran this code and all the console is showing me is "(lldb)".
Is it the syntax that I got wrong?
Or am I missing something here?
Also, I tried it different way using typedef:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
typedef enum {
no, yes
} boolean;
boolean boolVal = yes;
NSLog(#"%d", boolVal);
}
return 0;
}
I suspect I did something wrong with printing out the value, with NSLog().
But I have tried using %i, %#, %d. But the output was same, (lldb).
Are there any different ways to print out the enum values?
You have to give the members of the enum values is you want to print them. Try the following.
enum boolean {
no = 0,
yes = 1
};
NSLog(#"yes = %d",yes);
The previous code outputs the following.
yes = 1

"Programming in objective-c" 3rd Kochan exercise 4.2

Exercise:
Write a program that converts 27° from degrees Fahrenheit (F) to degrees Celsius
(C) using the following formula:
C = (F - 32) / 1.8
Note that you don’t need to define a class to perform this calculation. Simply evaluating
the expression will suffice.
Here is my code:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
float C;
float F;
F = 27;
C=(F-32)/1.8;
NSLog (#"27 degrees Fahrenheit is %f degrees Celsius." , C);
[drain pool];
return 0;
}
"Build failed"
On official forum there is a suggestion to write it this way :
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double C, F;
F=27;
C=(F-32)/1.8;
int c=C;
NSLog(#"%g degrees Fahrenheit equals %i centigrades!", F, c);
[pool drain];
return 0;
}
But it also gives me "Failed" message.
What is not correct?
Update
Problem resolved.
I didn't set up initial settings of my project properly.
I was working inside other "C" programming language project.
I had to just create new project-> OS X -> Command line tool (type: Foundation) unmark "Use Automatic Reference Counting"
But the best part- i was rewarded with successfully compiled program:
2012-08-09 00:20:29.214 4.2[19452:403] 27 degrees Fahrenheit is -2.777778 degrees Celsius.
Thank you #trojanfoe , #john.k.doe , #drewk , #hol
This works:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
float C;
float F;
F = 27.0;
C=(F-32.0)/1.8;
NSLog (#"27 degrees Fahrenheit is %f degrees Celsius." , C);
}
return 0;
}
So does this:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
double F=27.2;
double C=(F-32.0)/1.8;
NSLog(#"%g degrees Fahrenheit equals %g centigrade!", F, C);
[pool drain];
return 0;
}
As you are not using objects even this will do it. Plain C.
int main(int argc, const char * argv[])
{
double C, F;
F=27;
C=(F-32)/1.8;
int c=C;
printf("%g degrees Fahrenheit equals %i centigrades!", F, c);
return 0;
}
Do you need to parse the arguments and calculate based on those?

CoreMidi.framework sending midi commands

there is an app called FreeStyler, that you can control using midi commands. In my mac app I want to send midi signals.
Can someone show an example of this?
Elijah
This is what it took to send a note to my blofeld synth. I hope it helps. You can use MIDIObjectGetProperties to find the uniqueIDs for all the midi devices connected to your mac.
#import <Foundation/Foundation.h>
#import <CoreMIDI/CoreMIDI.h>
MIDIEndpointRef getEndpointWithUniqueID(MIDIUniqueID id){
MIDIObjectRef endPoint;
MIDIObjectType foundObj;
MIDIObjectFindByUniqueID(id, &endPoint, &foundObj);
return (MIDIEndpointRef) endPoint;
}
MIDIClientRef getMidiClient(){
MIDIClientRef midiClient;
NSString *outPortName =#"blofeldOut";
MIDIClientCreate((CFStringRef)outPortName, NULL, NULL, &midiClient);
return midiClient;
}
MIDIPortRef getOutPutPort(){
MIDIPortRef outPort;
NSString *outPortName =#"blofeldOut";
MIDIOutputPortCreate(getMidiClient(), (CFStringRef)outPortName, &outPort);
return outPort;
}
MIDIPacketList getMidiPacketList(){
MIDIPacketList packetList;
packetList.numPackets = 1;
MIDIPacket* firstPacket = &packetList.packet[0];
firstPacket->timeStamp = 0; // send immediately
firstPacket->length = 3;
firstPacket->data[0] = 0x90;
firstPacket->data[1] = 60;
firstPacket->data[2] = 64;
// TODO: add end note sequence
return packetList;
}
void play_note(void) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MIDIPacketList packetList=getMidiPacketList();
MIDIUniqueID blofeldEndpointID = -934632258;
MIDIEndpointRef blofeldEndpoint = getEndpointWithUniqueID(blofeldEndpointID);
MIDISend(getOutPutPort(), blofeldEndpoint, &packetList);
MIDIEndpointDispose(blofeldEndpoint);
[pool drain];
}
int main (int argc, const char * argv[]) {
play_note();
return 0;
}
Your application will need to use the CoreMIDI framework to send or receive MIDI, which I can tell you from experience is not a lot of fun to work with directly. You might want to try the vvopensource framework, which is a MIDI framework designed for cocoa.