Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Objective-C Online

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NSLog (@"hello world");
   
   
   
   
   [pool drain];
   return 0;
}

Compile and Execute Objective-C Online

#import <Foundation/Foundation.h>

@interface NSArray (Algorithm)
- (NSArray *)bubbleSort;
@end

@implementation NSArray (Algorithm)
// [1, 2, 3, 4, 5]
//           i 
//           j j + 1
- (NSArray *)bubbleSort {
    NSMutableArray *mutableCopyArray = [NSMutableArray arrayWithArray: self];
    int count = mutableCopyArray.count;
    int i;
    // for (i = 0; i < count - 1; i ++) {
    //     for (j = 0; j < count - 1; j ++) {
    //         NSLog(@"(%zd, %zd), %@", i, j, mutableCopyArray);
    //         if ([[mutableCopyArray objectAtIndex: j] integerValue] > [[mutableCopyArray objectAtIndex: j + 1] integerValue]) {
    //             [mutableCopyArray exchangeObjectAtIndex: j withObjectAtIndex: j + 1];
    //         }
    //     }
    // }
    BOOL swapped = YES;
    while (swapped) {
        swapped = NO;
        for (i = 1; i < count; i ++) {
            NSLog(@" i = %d, swapped = %zd, array : %@", i, swapped, mutableCopyArray);
            NSLog(@"now compare %@ and %@", [mutableCopyArray objectAtIndex: i - 1], [mutableCopyArray objectAtIndex: i]);
            if ([[mutableCopyArray objectAtIndex: i - 1] integerValue] > [[mutableCopyArray objectAtIndex: i] integerValue]) {
                [mutableCopyArray exchangeObjectAtIndex:(i-1) withObjectAtIndex:i];
                NSLog(@" swap with %d and %d, array : %@", i - 1, i, mutableCopyArray);
                swapped = YES;
            }
        }
    }
    return mutableCopyArray;
}

@end
@interface NSString (NumberFromString)
- (NSNumber *) numberFromString:(NSNumberFormatter *)formatter;
@end

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSArray *originalArray = [NSArray arrayWithObjects: [NSNumber numberWithInt: 5], [NSNumber numberWithInt: 4], [NSNumber numberWithInt: 3], [NSNumber numberWithInt: 2], [NSNumber numberWithInt: 1],nil];
   NSLog(@"%@", [originalArray bubbleSort]);
   NSLog(@"%zd", (BOOL)([[NSNumber numberWithInt: 5] integerValue] > [[NSNumber numberWithInt: 3] integerValue]));
   [pool drain];
   return 0;
}

Objective C Nested Switch Statement

#import <Foundation/Foundation.h>
 
int main () {
   
   /* local variable definition */
   int a = 100;
   int b = 200;
 
   switch(a) {
      case 100: 
         NSLog(@"This is part of outer switch:%d\n", a );
         switch(b) {
            case 200:
               NSLog(@"This is part of inner switch:%d\n", a );
         }
   }
   NSLog(@"Exact value of a is : %d\n", a );
   NSLog(@"Exact value of b is : %d\n", b );
 
   return 0;
}

Objective C Sizeof Operator

#import <Foundation/Foundation.h>

int main()
{
int n [5][2] = {{1,2},{2,3},{3,4},{4,5},{5,6}};
int i,j;

for (i = 0; i < 5 ; i++){
for (j = 0;j < 5 ; j++){
    NSLog(@"Values is n[%d] %d = %d\n",i,j,n[i][j]);
}
    
}
return 0;
}

Objective C Protocol

#import <Foundation/Foundation.h>

@protocol PrintProtocolDelegate
- (void)processCompleted;

@end

@interface PrintClass :NSObject {
   id delegate;
}

- (void) printDetails;
- (void) setDelegate:(id)newDelegate;
@end

@implementation PrintClass
- (void)printDetails {
   NSLog(@"Printing Details");
   [delegate processCompleted];
}

- (void) setDelegate:(id)newDelegate {
   delegate = newDelegate;
}

@end

@interface SampleClass:NSObject<PrintProtocolDelegate>
- (void)startAction;

@end

@implementation SampleClass
- (void)startAction {
   PrintClass *printClass = [[PrintClass alloc]init];
   [printClass setDelegate:self];
   [printClass printDetails];
}

-(void)processCompleted {
   NSLog(@"Printing Process Completed");
}

@end

int main(int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass startAction];
   [pool drain];
   return 0;
}

[email protected]

#import <Foundation/Foundation.h>

@interface SampleClass:NSObject
- (void)sampleMethod;
@end

@implementation SampleClass

- (void)sampleMethod {
   NSLog(@"Hello, World! \n");
}

@end

int main() {
   /* my first program in Objective-C */
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass sampleMethod];
   return 0;
}

PRASHANT

#import <Foundation/Foundation.h>

@protocol PrintProtocolDelegate
- (void)processCompleted;

@end

@interface PrintClass :NSObject {
   id delegate;
}

- (void) printDetails;
- (void) setDelegate:(id)newDelegate;
@end

@implementation PrintClass
- (void)printDetails {
   NSLog(@"Printing Details");
   [delegate processCompleted];
}

- (void) setDelegate:(id)newDelegate {
   delegate = newDelegate;
}

@end

@interface SampleClass:NSObject<PrintProtocolDelegate>
- (void)startAction;

@end

@implementation SampleClass
- (void)startAction {
   PrintClass *printClass = [[PrintClass alloc]init];
   [printClass setDelegate:self];
   [printClass printDetails];
}

-(void)processCompleted {
   NSLog(@"Printing Process Completed");
}

@end

int main(int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   SampleClass *sampleClass = [[SampleClass alloc]init];
   [sampleClass startAction];
   [pool drain];
   return 0;
}

Objective-C HelloWorldStringClass

#import <Foundation/Foundation.h>

@interface HelloWorldStringClass:NSObject

- (void)sayHello;

- (void)sayHelloIPhone;

- (void)sayHelloPHP;

- (void)countHelloChars;

@end



@implementation HelloWorldStringClass

- (void)sayHello {
   NSString *msg1 = @"Hello World Object C!";
   NSLog(@"Greeting Uppercase : %@ \n",[msg1 uppercaseString]);
}

- (void)sayHelloIPhone {
   NSString *msg2 = @"Hello World Object C For Iphone.";
   NSLog(@"Greeting lowercase : %@ \n",[msg2 lowercaseString]);
}

- (void)sayHelloPHP {
   NSLog(@"Hello World Php. \n");
}

- (void)countHelloChars {

   NSString *msg3 = @"Hello World";
   int len = [msg3 length];
   NSLog(@"Greeting Length : %@ - %d",msg3,len);
}


@end



int main() {
   /* my second  program in Objective-C */
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   HelloWorldStringClass *obj = [[HelloWorldStringClass alloc]init];
   
   
   [obj sayHello];
   
   [obj sayHelloIPhone];
   
   [obj sayHelloPHP];
    [pool drain];
   [obj countHelloChars];
   
   
   
   return 0;
}

Objective-C - Hello World

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NSLog (@"hello world");
   [pool drain];
   return 0;
}

MutableArray

#import <Foundation/Foundation.h>

@interface MutArrSample : NSObject
-(void)disp;
@end

@implementation MutArrSample
-(void)disp{
    // Mutable array with capcity
    NSMutableArray *sampleArr = [NSMutableArray arrayWithCapacity:1];
    [sampleArr addObject:@"America"];
    [sampleArr addObject:@"Australia"];
    NSLog(@"Sample Array %@",sampleArr);
    printf("Hello \n");
    
    //Init
    NSMutableArray *samp = [[NSMutableArray alloc]init];
    NSLog(@"samp %@",samp);
    
    //Init and add object to array
    NSMutableArray *samp1 = [[NSMutableArray alloc]initWithCapacity:1];
    [samp1 addObject:@"America"];
    [samp1 addObject:@"Japan"];
     NSLog(@"samp %@",samp1);
    [samp1 addObjectsFromArray:@[@"Eezy",@"Tutorials"]];
     NSLog(@"samp %@",samp1);
    
}
@end

int main(int argc, const char* argv[]){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    MutArrSample *sample = [[MutArrSample alloc]init];
    [sample disp];
    printf("Hello World \n");
    [pool drain];
    return 0;
}

1 2 3 4 5 6 7 ... 18 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.