Objective-C Programming

180

Solutions

207

Contributors

Objective-C is an object-oriented programming language that is a strict superset of C (any valid C code is equally valid Objective-C code), and it inherits its object-oriented capabilities from Smalltalk. All procedural syntax is identical to that of C, and all object-oriented syntax is an implementation of Smalltalk messaging. It is the main programming language used by Apple for OS-X and iOS and their respective APIs/ Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods.

Share tech news, updates, or what's on your mind.

Sign up to Post

I have next expressions:
1)      DataValue += (1<<i);
2)      if( ((DataValue>>i)&0x1) == 0x1 )
         {
            ; //Do something
         }
I need help to understand step by step what happens in the expressions above.
Could you write them in expanded format, simple line by line, eventually with some explanations?
0
In an IOS Swift application I have a function that is returning a date as a string like ("2016-04-18"). I need help with Swift code for a function that will take this string or any other string in this format (for any other date) and convert it into NSDate and return me that date as  a string in the following format "18 April 2016".
So if I give this function any other date in the format "2016-03-03" then I will get back "3 March 2016".
Thank you for your help.
0
good day.  i need some help with reading from a plist.  i am an objective-c noob and would appreciate any help and code examples.  i don't believe i am far off.

i have a root.plist with two text field items.

my code to read from the list:

 -(void)readConfigurationItemsFromSettingsBundle{
        NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
        if(!settingsBundle) {
            NSLog(@"Could not find Settings.bundle");
            return;
        }
        
        NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
        NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
   
        for (NSString *str in preferences)
          NSLog(@"--%@", str);
  }

Open in new window


here are my results:

 --{
    Key = "server_ip";
    Title = "server ip address";
    Type = PSTextFieldSpecifier;
}
--{
    Key = "audio_number";
    Title = "audio notification #";
    Type = PSTextFieldSpecifier;
}

i'm looking for the actual values within each key, but i don't know how to go about it....

thank you.
0
UINavigationController seems to not be working. The expected behavior is to click on one of the items in the table, and have it navigate to the next page. Furthermore, I was hoping to see a navigation bar at the top of the table, but I don't.

// AppDelegate.h
#import <UIKit/UIKit.h>
#import "TableViewControllerMusicians.h"
#import "ViewController2.h"
#import "ViewController3.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBar;

@property (strong, nonatomic) UINavigationController *nav;
@property (strong, nonatomic) TableViewControllerMusicians *tvMusicians;

@property (strong, nonatomic) ViewController2 *vc2;
@property (strong, nonatomic) ViewController3 *vc3;

Open in new window


// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] init];
    self.window.backgroundColor = [UIColor lightGrayColor];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    self.tabBar = [[UITabBarController alloc] initWithNibName:nil bundle:nil];

    self.tvMusicians = [[TableViewControllerMusicians alloc] initWithStyle:UITableViewStylePlain];
    self.tvMusicians.title = @"Musicians";
    
    self.nav = [[UINavigationController alloc] initWithRootViewController:self.tvMusicians];
    
    self.vc2 = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
    self.vc2.title = @"VC2";
    self.vc2.tabBarItem.title = @"My VC2";
    
    self.vc3 = [[ViewController3 alloc] initWithNibName:nil bundle:nil];
    self.vc3.title = @"VC3";
    self.vc3.tabBarItem.title = @"My VC3";
    
    
    self.tabBar.viewControllers = [NSArray arrayWithObjects: self.tvMusicians, self.vc2, self.vc3, nil];
    
    self.window.rootViewController = self.tabBar;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

Open in new window


// TableViewControllerMusicians.m

Open in new window

0
I'm stuck on my project. I started out by adding 3 UIViewControllers to the tab controller. Each view controller is simply a different color.

The first view controller automatically shows the correct color, but when I click on the other two tabs, it appears that they are not opening those views, as the color never changes.

didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] init];
    self.window.backgroundColor = [UIColor lightGrayColor];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    self.vc1 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    self.vc1.title = @"VC1";
    
    self.vc2 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    self.vc2.title = @"VC2";
    
    self.vc3 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    self.vc3.title = @"VC3";
    
    
    self.tabBar = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
    self.tabBar.viewControllers = [NSArray arrayWithObjects: self.vc1, self.vc2, self.vc3, nil];
    
    self.window.rootViewController = self.tabBar;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

Open in new window


AppDelegate.h:
#import <UIKit/UIKit.h>
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBar;
@property (strong, nonatomic) ViewController1 *vc1;
@property (strong, nonatomic) ViewController1 *vc2;
@property (strong, nonatomic) ViewController1 *vc3;


@end

Open in new window



ViewController1:

#import "ViewController1.h"

@interface ViewController1 ()

@property() UIView *myView;

@end

@implementation ViewController1


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myView = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 20.0, 20.0)];
    self.myView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.myView];
}

@end

Open in new window


ViewController2 and ViewController3 are exactly the same code as ViewController1 except the backgroundColor is different.
0
I have a background in .NET, but am new to iOS development (objective C).

What is the meaning of the strong vs. weak attributes of properties as in:

@property(strong) NSString *make;
@property(weak) NSString *make;
0
Hi, I have a question regarding how to properly enumerate through a hard drive's file system and identify which artifact are a "Directory" and which are a "File." Below I've included my code for your inspection. I've been able to successfully enumerate through the selected path (which for testing purposes is hard coded), but the part I'm struggling with is how to identify which entry is a Directory and which is simply a regular File.

With this code I get that all the results are "NSFileTypeDirectory", when in fact I know that some are and some aren't. I know I must be missing something very basic - probably, because I'm quite new to Objective-C.

Can someone please shed some light on this matter and determine what it is that I'm doing wrong.

Thank you,
Fulano

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
       
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSString * path = @"/Users/Bob/Desktop/FileStorage";                       // (Hard coded for testing only)
        NSString * nextPath;
        NSDirectoryEnumerator * dirEnumerator = [fileManager enumeratorAtPath:path];
        NSDictionary * dict = [fileManager attributesOfItemAtPath:path error:nil];
        for (nextPath in dirEnumerator) {
                       
                NSLog(@"> %@ -- %@", nextPath, [dict valueForKey:@"NSFileType"]);
           
        }
    }
        
    return 0;
}

Open in new window

0
Hi All,

I have a GUID : 373D06E9-587E-4930-B846-12500FF1AC2F which needs to be converted into hexadecimal using Cocoa or objective C.



Can some one please let me know how to do that.

Thanks,
Ravi
0
I am on a short timeline and need advertising on my Objective-C iPhone app, and quick.

I may choose one of:
Google AdSense
Performix
Commission Junction

is any of these easier to add in than the other?

What changes do I need to make to my app?
How much coding is required?
Any potential problems from Apple when I submit the app update?


Thanks!
0
Hi

I am writing a Mac Console App and I need to read in a string of undefined length and assign it to an NSString*. Anybody know an easy way?

Kind Regards,
Adrian Smith
0
I need to update an iPhone app to find valid web URL's inside text blocks. So I need a Regular Expression to do that for me.

Is this the same Regular Expression format used in C#? I think there are different variants o the Regular Expression syntax.

Please advice.

Thanks.
0
I need to create a function with the same input params as NSLOG so that inside I can call NSLOG. Then when I am ready to submit my all to the app store, I simply comment out one line of code to disable all logging.

I will not need to use the name NSLOG, and could call it MyLog() for example.

What is wrong here?

// BTLog.h
#import <Foundation/Foundation.h>

@interface BTLog : NSObject
{
   
}
+(void)MyLog:(NSString*)logMessage;
@end

//BTLog.m
@implementation BTLog

+(void)MyLog:(NSString*)logMessage
{
    NSLog(logMessage);
}
@end

I get the following error when I try and use it:

"Property 'MyLog' not found on object of type 'BTLog'
0
I need to update certain value in an XML file in order to upgrade files to a newer version of my software. So is there an easy way to open an XML file and change certain node names?

Thanks.
0
I am very new to Cocoa and Objective C.  I have years of Windows programming experience, but I am finding it quite difficult to make the leap.  I have been reading a lot, but progress is slow...

I created a minimal app with the wizard.  
All I am trying to do is get it to respond to the up arrow key press event.
I didn't add any custom views (is this part of the problem?)
I must be missing something obvious...

Here is my code -

AppDelegate.h -
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
- (BOOL)acceptsFirstResponder;
- (IBAction)moveUp:(id)sender;
@end

AppDelegate.m -
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
- (BOOL)acceptsFirstResponder {
    return YES;
}
-(IBAction)moveUp:(id)sender {
    NSLog(@"up arrow key was pressed");
}
@end
0
I'm having a hard time working through an program in a book.  

Book: Programming in objective-C (fourth edition) by Stephen G. Kochan
Program: 16-6

What I'm trying to do: pass arguments and output a list of files that are in the current directory

// Partial program code

NSFileManager *fm;
NSString *source, *dest;
BOOL isDir;
NSProcessInfo *proc = [NSProcessInfo];
NSArray *args = [proc arguments];

fm = [NSFileManager defaultManager];

//Check for two arguments on the command line
if ([args count] != 3)
{

NSLog(@"Usage: %@ src dest", [proc processName]);
return 1;  

}

source = [args objectAtIndex: 1];
dest = [args objectAtIndex: 2];

// Make sure the source file can be read
if ([fm isReadableFileAtPath: source] == NO)
{

NSLog (@"Can't read %@", source);

}
....
....

The problem I have is the program never gets past the last if statement.  and in the output window It displays the string - 'Cant read ls'

In the book the author doesn't explain how to pass arguments for this example.  But in the book it looks as if the author is passing $ls -l and displaying  a list of files in output window.

After doing some research its my understanding that I need to goto
Product > Scheme > Edit Scheme
To pass arguments

In the list of arguments passed on Launch, I have added
ls -l

I set a breakpoint at the last if statement and viewed the values of the arguments
args [0] = "../../.../../Program 16.6"
args [1] = @"ls"
args [2] = @"-l"
0
I'm an experienced developer with C++, C# for mainstream windows PC's.
I'd like to convert a couple of ideas into apps for mobile devices such as android based phones, iPod/iPad with apple OS's and, possibly, windows tablets.

What tools would you recommend for a development environment, programming language and emulator to achieve the above.  (Obviously I would prefer one common IDE with one code base and emulator for all OP systems but that might not be possible.)
0
I need a tool to support my development.  

What is recommended?  It would be nice if I could integrate it into SourceTree.

Thanks.
0
I have n exception in this code (which I did not write):

this class is not key value coding-compliant for the key 'questionGuid'.

I suspect the object i the wrong type, but need to understand the code a bit.

Can someone explain it to me?



 NSPredicate* predicate = [NSPredicate predicateWithFormat:@"questionGuid LIKE[c] %@ ", [_result question].guid];

QuestionAsked* questionAsked = [[_quizEngine.theme.questionAskedArr filteredArrayUsingPredicate:predicate] objectAtIndex:0];

[questionAsked updateResultArray:[submitTapDate timeIntervalSince1970] isCorrect:_result.isAnswerCorrect];
0
I am new to Objective C. I want to set a breakpoint and when execution stops at the breakpoint I want to step through the code to see the flow of the execution. When using debuggers on the Windows platform I could set a breakpoint and then step through the code using F8 or F7. I would like to do the same using X Code. I have looked in the documentation of X Code as well as explored the internet in search of a solution, but cannot find one.
0
I am new to ObjC and need to sort my object which I use to fill an NSMutableArray.

My object contains a timestamp and I'd like to sort in either Ascending or Descending order. I am not sure where to locate the Sort Selector.

Could someone please make up the various parts and show me?

Thanks.
0
In what sequence does Objective-C execute 2 expressions which make up a single IF statement?  

In C++, either expression could be the first to execute. So, the following is a bad idea in C++:

if ( questionsWithSubmitHistory != nil && questionsWithSubmitHistory.count > 0)

How about Objective-C?
0
I need to create a base class with 2 methods, a public virtual property and a private variable.  Then I need two child classes which override one method and one method.  

method #1 - virtual, overridden in child class
method #2 - private, exists only in base class and called by child classes
property #1 - public virtual
variable #1 - private, used by methods in base class and child classes

Thanks.
0
Please provide a little sample code showing how to create, size appropriately and release arrays using Objective-C.

I want to make a function that sorts an existing array and returns the sorted array.  But since I am not using ARC, I need to be sure to free up all memory used in the process.

So, rather than creating the array inside the method, I would like to pass in an empty array already constructed. Then, the calling function, which created the array, will be responsible to release that array as well.

Does this make sense?

I need the syntax to to this.

Thanks.
0
Learning Objective C. Attempting to create an initialization method:

//Code from Fraction.m
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(id) init
{
       return [self initWith: 0 over: 0];
}

-(Fraction *) initWith: (int) n over: (int) d
{
    self = [super init];
   
    if (self)
    {
        [self setTo: n over: d];
    }
   
    return self;    
}

Error Message before compiling:

//From main.m

Fraction *a, *b;

a = [[Fraction alloc] initWith: 7 over: 17];       //This line causes the Error Message below.

Error Message: "Implicit conversion of 'int' to 'id' is disallowed with ARC.
0
I am used to programming with Visual Studio and C#.  And I understand there are difference with the way Objective-C handles the binding a call to a method, but do need to find where a given method is "messaged".  

Is there a way using Xcode to find which objects message a method, and where?

Thanks.
0

Objective-C Programming

180

Solutions

207

Contributors

Objective-C is an object-oriented programming language that is a strict superset of C (any valid C code is equally valid Objective-C code), and it inherits its object-oriented capabilities from Smalltalk. All procedural syntax is identical to that of C, and all object-oriented syntax is an implementation of Smalltalk messaging. It is the main programming language used by Apple for OS-X and iOS and their respective APIs/ Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods.

Top Experts In
Objective-C Programming
<
Monthly
>

No Top Experts for this time period. Answer questions to earn the title!