Welcome, Guest. Please login or register.
Did you miss your activation email?
April 29, 2016, 04:33:39 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Recent Posts
Pages: 1 2 [3] 4 5 ... 10

 21 
 on: December 31, 2015, 05:19:16 AM 
Started by anhe - Last post by anhe
Hello all,

and this is my solution for exercise 16.7. In fact it is a simple variation of 16.6 with a substitution of

Code: (Objective-C)
outFile = [NSFileHandle fileHandleForWritingAtPath:@"testout"];

with:

Code: (Objective-C)
outFile = [NSFileHandle fileHandleWithStandardOutput];
.

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        // Get instance of NSFilemanager and get current directory path
        NSFileManager *fm = [NSFileManager defaultManager];
        NSString *path = [fm currentDirectoryPath];
        NSLog(@"Current directory path is: %@", path);
       
        NSFileHandle *inFile, *outFile;
        NSData *buffer = nil;
       
        // Open the file test for reading
        inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile3"];
       
        if (inFile == nil) {
            NSLog(@"Open of testfile for reading failed");
            return 1;
        }
       
        // Create the output file first if necessary
        [fm createFileAtPath:@"testout" contents:nil attributes:nil];
       
        // Now open outfile for writing
        outFile = [NSFileHandle fileHandleWithStandardOutput];
       
        NSInteger kBufSize = 128, kBufRead = 0;
   
        buffer = [inFile readDataOfLength:kBufSize];

       
        while ([buffer length] !=0) {
            NSLog(@"%li / %li", kBufRead, kBufSize);
            [inFile seekToFileOffset:kBufRead];
            buffer = [inFile readDataOfLength:kBufSize];
            [outFile writeData:buffer];
            kBufRead += kBufSize;
        }
       
        // Close the inFile
        [inFile closeFile];
       
    }
    return 0;
}


kind regards and my best wishes for 2016  Cheesy

André

 22 
 on: December 31, 2015, 04:13:05 AM 
Started by anhe - Last post by anhe
Hello all,

this is my solution for exercise 16.6:

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        // Get instance of NSFilemanager and get current directory path
        NSFileManager *fm = [NSFileManager defaultManager];
        NSString *path = [fm currentDirectoryPath];
        NSLog(@"Current directory path is: %@", path);
       
        NSFileHandle *inFile, *outFile;
        NSData *buffer = nil;
       
        // Open the file test for reading
        inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile3"];
       
        if (inFile == nil) {
            NSLog(@"Open of testfile for reading failed");
            return 1;
        }
       
        // Create the output file first if necessary
        //[[NSFileManager defaultManager] createFileAtPath:@"testout" contents:nil attributes:nil];
        [fm createFileAtPath:@"testout" contents:nil attributes:nil];
       
        // Now open outfile for writing
        outFile = [NSFileHandle fileHandleForWritingAtPath:@"testout"];
       
        if (outFile == nil) {
            NSLog(@"Open of testout for writing failed");
            return 2;
        }
       
        NSInteger kBufSize = 100, kBufRead = 0;
       
        // Truncate the output file since it may contain data
        [outFile truncateFileAtOffset:0];
        buffer = [inFile readDataOfLength:kBufSize];
       
        while ([buffer length] !=0) {
            NSLog(@"%li / %li", kBufRead, kBufSize);
            [outFile truncateFileAtOffset:kBufRead];
            [inFile seekToFileOffset:kBufRead];
            buffer = [inFile readDataOfLength:kBufSize];
            [outFile writeData:buffer];
            kBufRead += kBufSize;
            NSLog(@"Incremental Part of buffer: %@", [[NSString alloc]initWithData:buffer encoding:NSUTF8StringEncoding]);
        }
       
        // Close both files
        [inFile closeFile];
        [outFile closeFile];
       
        // Verify the file's contents
        NSLog(@"Final output: %@", [NSString stringWithContentsOfFile:@"testout" encoding:NSUTF8StringEncoding error:NULL]);
       

    }
    return 0;
}


kind regards

André

 23 
 on: December 28, 2015, 02:04:37 AM 
Started by anhe - Last post by anhe
Hello all,

this is my solution for exercise 16.5:

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface NSString (TempFiles)
+(NSString *) temporaryFileName;

@end

@implementation NSString (TempFiles)

+(NSString *) temporaryFileName
{
    NSString *tmpFileName = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]];
   
    return tmpFileName;
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSLog(@"Temporary file name: %@",[NSString temporaryFileName]);
    }
    return 0;
}


kind regards

André

 24 
 on: December 27, 2015, 01:40:54 PM 
Started by anhe - Last post by anhe
Hello all,

this is my solution for exercise 16.4:

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSProcessInfo *myProc = [NSProcessInfo processInfo];
       
        // get commandline arguments
        NSArray *myArgs = [myProc arguments];
        NSLog(@"%@", myArgs);
       
        // get environment variables
        NSDictionary *myEnvironment = [myProc environment];
        //NSLog(@"%@", myEnvironment);
       
        // get environment-variable for key "Apple_PubSub_Socket_Render"
        NSString *myAPSSR = [myEnvironment objectForKey:@"Apple_PubSub_Socket_Render"];
        NSLog(@"Apple_PubSub_Socket_Render = %@", myAPSSR);
       
        // get environment-variable for key "DYLD_FRAMEWORK_PATH"
        NSString *myDFP = [myEnvironment objectForKey:@"DYLD_FRAMEWORK_PATH"];
        NSLog(@"DYLD_FRAMEWORK_PATH = %@", myDFP);
       
        // get environment-variable for key "DYLD_LIBRARY_PATH"
        NSString *myDLP = [myEnvironment objectForKey:@"DYLD_LIBRARY_PATH"];
        NSLog(@"DYLD_LIBRARY_PATH = %@", myDLP);
       
        // get environment-variable for key "HOME"
        NSString *myHome = [myEnvironment objectForKey:@"HOME"];
        NSLog(@"HOME = %@", myHome);
       
        // get environment-variable for key "LOGNAME"
        NSString *myLOGNAME = [myEnvironment objectForKey:@"LOGNAME"];
        NSLog(@"LOGNAME = %@", myLOGNAME);
       
        // get environment-variable for key "MallocNanoZone"
        NSString *myMNZ = [myEnvironment objectForKey:@"MallocNanoZone"];
        NSLog(@"MallocNanoZone = %@", myMNZ);
       
        // get environment-variable for key "NSUnbufferedIO"
        NSString *myNSUIO = [myEnvironment objectForKey:@"NSUnbufferedIO"];
        NSLog(@"NSUnbufferedIO = %@", myNSUIO);
       
        // get environment-variable for key "PATH"
        NSString *myPATH = [myEnvironment objectForKey:@"PATH"];
        NSLog(@"PATH = %@", myPATH);
       
        // get environment-variable for key "PWD"
        NSString *myPWD = [myEnvironment objectForKey:@"PWD"];
        NSLog(@"PWD = %@", myPWD);
       
        // get environment-variable for key "SHELL"
        NSString *mySHELL = [myEnvironment objectForKey:@"SHELL"];
        NSLog(@"SHELL = %@", mySHELL);
       
        // get environment-variable for key "SSH_AUTH_SOCK"
        NSString *mySSHAS = [myEnvironment objectForKey:@"SSH_AUTH_SOCK"];
        NSLog(@"SSH_AUTH_SOCK = %@", mySSHAS);
       
        // get environment-variable for key "TMPDIR"
        NSString *myTMPDIR = [myEnvironment objectForKey:@"TMPDIR"];
        NSLog(@"TMPDIR = %@", myTMPDIR);
       
        // get environment-variable for key "USER"
        NSString *myUSER = [myEnvironment objectForKey:@"USER"];
        NSLog(@"USER = %@", myUSER);
       
        // get environment-variable for key "XPC_FLAGS"
        NSString *myXPCFLAGS = [myEnvironment objectForKey:@"XPC_FLAGS"];
        NSLog(@"XPC_FLAGS = %@", myXPCFLAGS);
       
        // get environment-variable for key "XPC_SERVICE_NAME"
        NSString *myXPCSNAME = [myEnvironment objectForKey:@"XPC_SERVICE_NAME"];
        NSLog(@"XPC_SERVICE_NAME = %@", myXPCSNAME);
       
        // get environment-variable for key "__CF_USER_TEXT_ENCODING"
        NSString *myCFUTE = [myEnvironment objectForKey:@"__CF_USER_TEXT_ENCODING"];
        NSLog(@"__CF_USER_TEXT_ENCODING = %@", myCFUTE);
       
        // get environment-variable for key "__XCODE_BUILT_PRODUCTS_DIR_PATHS"
        NSString *myXCODEBPDP = [myEnvironment objectForKey:@"__XCODE_BUILT_PRODUCTS_DIR_PATHS"];
        NSLog(@"__XCODE_BUILT_PRODUCTS_DIR_PATHS = %@", myXCODEBPDP);
       
        // get environment-variable for key "__XPC_DYLD_FRAMEWORK_PATH"
        NSString *myXPCDFP = [myEnvironment objectForKey:@"__XPC_DYLD_FRAMEWORK_PATH"];
        NSLog(@"__XPC_DYLD_FRAMEWORK_PATH = %@", myXPCDFP);
       
        // get environment-variable for key "__XPC_DYLD_LIBRARY_PATH"
        NSString *myXPCDLP = [myEnvironment objectForKey:@"__XPC_DYLD_LIBRARY_PATH"];
        NSLog(@"__XPC_DYLD_LIBRARY_PATH = %@", myXPCDLP);
       
        // get processIdentifier
        NSNumber *myProcIdentifier = [NSNumber numberWithInt:[myProc processIdentifier]];
        NSLog(@"processidentifier = %li", [myProcIdentifier integerValue]);
       
        // get processName
        NSString *myProcName = [myProc processName];
        NSLog(@"processName = %@", myProcName);
       
        // get globallyUniqueString
        NSString *myGUString = [myProc globallyUniqueString];
        NSLog(@"globallyUniqueString = %@", myGUString);
       
        // get hostname
        NSString *myHostName = [myProc hostName];
        NSLog(@"hostname = %@", myHostName);
       
        // get operatingSystemVersion
        NSOperatingSystemVersion myOSVersion = [myProc operatingSystemVersion];
        NSInteger major = myOSVersion.majorVersion;
        NSInteger minor = myOSVersion.minorVersion;
        NSInteger patch = myOSVersion.patchVersion;
       
        NSLog(@"myOSVersion = %li.%li.%li", major, minor, patch);
       
        // get operatingSystemVersionString
        NSString *myOSVersionString = [myProc operatingSystemVersionString];
        NSLog(@"operatingSystemVersionString = %@", myOSVersionString);
       
        // get processor count of hardware
        NSUInteger myProcCount = [myProc processorCount];
        NSLog(@"ProcessorCount = %li", myProcCount);
       
        // get active processor count of hardware
        NSUInteger myActiveProcCount = [myProc activeProcessorCount];
        NSLog(@"activeProcessorCount = %li", myActiveProcCount);
       
        // get physicalMemory of hardware
        NSUInteger myPhMemory = [myProc physicalMemory];
        NSLog(@"physical memory = %li bytes", myPhMemory);
       
    }
    return 0;
}


kind regards

André

 25 
 on: December 26, 2015, 02:21:24 PM 
Started by anhe - Last post by anhe
Hello all,

these are my solutions for exercises 16.3a (basename) and 16.3b (dirname):

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        // Define instances of NSProcessInfo
        NSProcessInfo *myProc = [NSProcessInfo processInfo];
        NSArray *myArgs = [myProc arguments];
       
        // check if number of Arguments is one or not
        if ([myArgs count] != 2) {
            NSLog(@"Usage: Exercise16.2 string");
            return 1;
        }
       
        // set argument to string
        NSString *string = myArgs[1];
       
        // original version of basename did not check if directory or file still exist
        /*if ([myFM fileExistsAtPath:string]==NO) {
            NSLog(@"Error: File or directory does not exists");
            return 2;
        }*/
       
        // get/set basename
        NSString *basename = [string lastPathComponent];
        NSLog(@"%@", basename);
       
    }
    return 0;
}


Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        // Define instances of NSProcessInfo
        NSProcessInfo *myProc = [NSProcessInfo processInfo];
        NSArray *myArgs = [myProc arguments];
       
        // check if number of Arguments is one or not
        if ([myArgs count] != 2) {
            NSLog(@"Usage: Exercise16.2 string");
            return 1;
        }
       
        // set argument to string
        NSString *string = myArgs[1];
       
        // original dirname did not check if directory or file still exist
        /*if ([myFM fileExistsAtPath:string]==NO) {
            NSLog(@"Error: File or directory does not exists");
            return 2;
        }*/
       
        // get/set dirname
        NSString *dirname = [string stringByDeletingLastPathComponent];
        NSLog(@"%@", dirname);
       
    }
    return 0;
}

kind regards

André

 26 
 on: December 26, 2015, 11:37:32 AM 
Started by anhe - Last post by anhe
Hello all,

this is my solution for exercise 16.2:

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
   
        // get instance of NSFileManager and NSProcessInfo
        NSFileManager *myFM = [NSFileManager defaultManager];
        NSProcessInfo *myProc = [NSProcessInfo processInfo];
        NSArray *myArgs = [myProc arguments];
       
        // get current directory path
        NSString *currentPath = [myFM currentDirectoryPath];
        NSLog(@"%@", currentPath);
       
        // check if number of arguments is identical to 3
        if ([myArgs count]!=3) {
            NSLog(@"Usage: Exercise16.2 directory filename");
            return 1;
        }
       
        // set first argument (myArgs[1]) to search-directory and second (myArgs[2]) to filename to locate
        NSString *searchDir = myArgs[1];
        NSString *fileToLocate = myArgs[2];
       
        // check if search-directory is a directory
        BOOL isDir;
        [myFM fileExistsAtPath:searchDir isDirectory:&isDir];
        if (isDir == NO) {
            NSLog(@"Error: first argument is not a directory!");
            return 2;
        }
       
        // enumerating each file in the directory
        NSDirectoryEnumerator *dirEnum = [myFM enumeratorAtPath:searchDir];

        // check if elements are identical to fileToLocate and add it to searchResults
        NSMutableArray *searchResults = [NSMutableArray array];
        while ((currentPath = [dirEnum nextObject]) != nil) {
            if ([currentPath rangeOfString:fileToLocate].location != NSNotFound) {
                [searchResults addObject:currentPath];
            }
        }
       
        // check if searchResultsArray is empty
        if ([searchResults count]==0) {
            NSLog(@"No results for %@", fileToLocate);
        }
        else{
            for (NSString *files in searchResults) {
                NSLog(@"%@%@", searchDir, files);
            }
        }
    }
    return 0;
}


kind regards

André

 27 
 on: December 25, 2015, 11:53:25 AM 
Started by anhe - Last post by anhe
Hello all,

this is my solution for exercise 16.1:

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        // get current directory path
        NSFileManager *fm = [NSFileManager defaultManager];
        NSString *path = [fm currentDirectoryPath];
        NSLog(@"Current directory path: %@", path);
       
        // get process info and number of arguments
        NSProcessInfo *myProc = [NSProcessInfo processInfo];
        NSArray *args = [myProc arguments];
        NSString *dest = [args lastObject];
   
        // Check of numbers of arguments
        if ([args count]<=2) {
            NSLog(@"Invalid number of arguments!");
            return 1;
        }
       
        // Define source-files (subarray of args)
        NSArray *sources = [args subarrayWithRange:NSMakeRange(1, [args count]-2)];
       
       
        // Check if all source file(s) can be read
        for (NSString *files in sources) {
            if ([fm isReadableFileAtPath:files]==NO) {
                NSLog(@"Can't read %@", files);
                return 3;
            }
        }
       
        // Check if destination is a directory
        BOOL isDir;
        [fm fileExistsAtPath:dest isDirectory:&isDir];
       
        if (isDir == NO && [args count]>3) {
            NSLog(@"last argument must be a directory");
            return 3;
        }
       
       
        // If destination is not a directory, there could be only one source file (number of arguments == 3)
        if (isDir == NO && [args count]==3) {
            if ([fm fileExistsAtPath:dest]== YES && [fm removeItemAtPath:dest error:NULL] == NO) {
                NSLog(@"Destination file already exists, and can not be removed!");
                return 4;
            }
           
            if ([fm copyItemAtPath:args[1] toPath:dest error:NULL] == NO) {
                NSLog(@"Copy failed!");
                return 5;
            }
        }
       
        // If destination is a directory
        if (isDir == YES && [args count]>=3) {
            for (NSString *files in sources) {
                NSString *tmp = [NSString stringWithString:dest];
                tmp = [dest stringByAppendingPathComponent:[files lastPathComponent]];
                if ([fm copyItemAtPath:files toPath:tmp error:NULL]==NO) {
                    NSLog(@"Copy of %@ failed!", files);
                    return 6;
                }
            }
        }
       
        // Give feedback of success
        for (NSString *files in sources) {
            NSLog(@"Copy of %@ to %@ succeeded", files, dest);
        }
       
    }
    return 0;
}


kind regards

André

 28 
 on: December 23, 2015, 07:43:05 AM 
Started by KBucevski - Last post by KBucevski
Here is how i did the exercises. What do you think? Tongue


MAIN-------------------

#import <Foundation/Foundation.h>
#import "Calculator.h"
#import "Calculator+Trig.h"
int main (int argc, char * argv[]) {
    @autoreleasepool {
        double value1, value2;
        char operator;
        int first;
        char tOperation;
        Calculator *myCalc = [[Calculator alloc]init];
       
        NSLog(@"Arithmetic = 1 or Trigonometry = 2");
        scanf("%i", &first);
       
        if (first == 1){
       
        NSLog(@"Type in your expression.");
        scanf("%lf %c %lf", &value1, &operator, &value2);
       
        [myCalc setAccumulator:value1];
   
        switch (operator){
               
                case '+':
                [myCalc add: value2];
                break;
               
                case '-':
                [myCalc subtract: value2];
                break;
               
                case '*':
                case 'x':
                case 'X':
                [myCalc multiply: value2];
                break;
               
                case '/':
                [myCalc divide: value2];
                break;
               
                default:
                NSLog(@"Unkown Opertaion!");
        }
   
        NSLog(@"=  %.2lf", [myCalc accumulator]);
        }
        else if (first == 2){
            NSLog(@"Enter the number and the first letter of the trigonomtry operation(cos = c, sin = s, tg = t, ctg = g))");
            scanf("%lf %s", &value1, &tOperation);
           
            [myCalc setAccumulator: value1];
           
            switch (tOperation) {
                case ('c'):
                    NSLog(@"cos of %f is %lf", value1, [myCalc cos: value1]);
                    break;
                case ('s'):
                    NSLog(@"sin of %f is %lf", value1, [myCalc sin: value1]);
                    break;
                case ('t'):
                    NSLog(@"tan of %f is %lf", value1, [myCalc tan: value1]);
                    break;
                case ('g'):
                    NSLog(@"ctg of %f is %lf", value1, [myCalc ctg: value1]);
                    break;
                default:
                    NSLog(@"Unkown Opertaion!");
                    break;
            }
           
           
           
        }
       
        else {
            NSLog(@"Restart the program");
        }
       
       
}
return 0;
}

CALCULATOR+TRIG.h----------------------------

#import "Calculator.h"

@interface Calculator (Trig)

-(double) sin: (double) value;
-(double) cos: (double) value;
-(double) tan: (double) value;
-(double) ctg: (double) value;


@end
CALCULATOR+TRIG.m----------------------------

#import "Calculator+Trig.h"

@implementation Calculator (Trig)

-(double) sin: (double) value{
    double result;
    result = sin(value);
    return result;
}
-(double) cos: (double) value{
    double result;
    result = cos(value);
    return result;
}
-(double) tan: (double) value{
    double result;
    result = tan(value);
    return result;
}
-(double) ctg: (double) value{
    double result;
    result = tan(value);
    return  1/result;
}


@end

 29 
 on: December 20, 2015, 04:13:24 AM 
Started by skochan - Last post by ivanDub
It all goes to show you that there is NOTHING under the sun that is Beatle Proof

Steve Jobs in his younger days reminded me somewhat of John Lennon.

___123___Post reply___123___

 30 
 on: December 19, 2015, 10:55:02 AM 
Started by skochan - Last post by mmoore
I found this site by way of your book, Programming in Objective C, 6th ed. I have been working through the chapters and have found it pretty self explanatory until I ran into difficulties in Chap8 with the use of dynamic typing and the dot operator, as well as Chap11 with the use of separate files for categories and the use of instance variables. Both time I have found what I have needed quite easily. Thanks for setting up this site. It is very useful.

Pages: 1 2 [3] 4 5 ... 10


Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.