Questions tagged [objective-c]

This tag should be used only on questions that are about Objective-C features or depend on code in the language. The tags [cocoa] and [cocoa-touch] should be used to ask about Apple's frameworks or classes. Use the related tags [ios], [macos], [apple-watch] and [tvos] for issues specific to those platforms.

Objective-C is an object-oriented programming language combining features of C () and Smalltalk (). It is a strict superset of C (any valid C code is equally valid Objective-C code, with the minor exception that id is a free identifier for user use in C while it is a keyword in Objective-C), 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.

Objective-C was created primarily by Brad Cox and Tom Love in the early 1980s at their company Stepstone. It is now primarily developed by Apple, Inc.

Objective-C is a general-purpose, high-level, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for and and their respective APIs, and .

Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods. It also adds language-level support for object graph management and object literals while providing dynamic typing and binding, deferring many responsibilities until run time.

Hello World in Objective-C

#import <Foundation/Foundation.h>

int main(void)
{
    NSLog(@"Hello World!");
    return 0;
}

Syntax

Objective-C is a thin layer on top of C, and moreover is a strict superset of C; it is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class. Objective-C derives its object syntax from Smalltalk. All of the syntax for non-object-oriented operations (including primitive variables, pre-processing, expressions, function declarations, and function calls) is identical to that of C, while the syntax for object-oriented features is an implementation of Smalltalk-style messaging.

Objective-C has many powerful features as detailed below:


References

Frequently posted questions in the Objective-C tag

Books

Video Tutorial

  • Developing iOS 7 Apps for iPhone and iPad by Stanford University - FREE
    From the site, "Tools and APIs required to build applications for the iPhone and iPad platform using the iOS SDK. User interface designs for mobile devices and unique user interactions using multi-touch technologies. Object-oriented design using model-view-controller paradigm, memory management, Objective-C programming language." (Similar courses are also available for iOS 8 & 9, but only in Swift)

Related Tags

292452 questions
365
votes
28 answers

Get the current first responder without using a private API

I submitted my app a little over a week ago and got the dreaded rejection email today. It tells me that my app cannot be accepted because I'm using a non-public API; specifically, it says, The non-public API that is included in your application is…
Justin Kredible
  • 8,354
  • 15
  • 65
  • 91
363
votes
18 answers

How can I reverse a NSArray in Objective-C?

I need to reverse my NSArray. As an example: [1,2,3,4,5] must become: [5,4,3,2,1] What is the best way to achieve this?
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
363
votes
12 answers

Best way to define private methods for a class in Objective-C

I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods. I understand there may be several conventions and habits and think about this question as an…
Yurii Soldak
  • 1,458
  • 4
  • 19
  • 24
358
votes
16 answers

How do I draw a shadow under a UIView?

I'm trying to draw a shadow under the bottom edge of a UIView in Cocoa Touch. I understand that I should use CGContextSetShadow() to draw the shadow, but the Quartz 2D programming guide is a little vague: Save the graphics state. Call the function…
Fraser Speirs
  • 4,642
  • 3
  • 21
  • 15
358
votes
14 answers

Generate JSON string from NSDictionary in iOS

I have a dictionary I need to generate a JSON string by using dictionary. Is it possible to convert it? Can you guys please help on this?
Sekhar Bhetalam
  • 4,501
  • 6
  • 33
  • 52
355
votes
8 answers

Use of alloc init instead of new

Learning Objective-C and reading sample code, I notice that objects are usually created using this method: SomeObject *myObject = [[SomeObject alloc] init]; instead of: SomeObject *myObject = [SomeObject new]; Is there a reason for this, as I have…
willc2
  • 38,991
  • 25
  • 88
  • 99
355
votes
6 answers

Difference between objectForKey and valueForKey?

What is the difference between objectForKey and valueForKey? I looked both up in the documentation and they seemed the same to me.
Devoted
  • 177,705
  • 43
  • 90
  • 110
350
votes
18 answers

Instantiate and Present a viewController in Swift

Issue I started taking a look on the Swift Programming Language, and somehow I am not able to correctly type the initialization of a UIViewController from a specific UIStoryboard. In Objective-C I simply write: UIStoryboard *storyboard =…
E-Riddie
  • 14,660
  • 7
  • 52
  • 74
348
votes
8 answers

When to use NSInteger vs. int

When should I be using NSInteger vs. int when developing for iOS? I see in the Apple sample code they use NSInteger (or NSUInteger) when passing a value as an argument to a function or returning a value from a function. - (NSInteger)someFunc;... -…
Shizam
  • 9,627
  • 8
  • 51
  • 82
348
votes
17 answers

How to scale a UIImageView proportionally?

I have a UIImageView and the objective is to scale it down proportionally by giving it either a height or width. UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL…
Ronnie Liew
  • 18,220
  • 14
  • 46
  • 50
347
votes
42 answers

library not found for -lPods

I got an error when archiving a project. This is my environment. Mac OS Lion Xcode 4.3.1 iOS SDK 5.1 The project deployment target is: IPHONEOS_DEPLOYMENT_TARGET 3.2 The error shows: ld: library not found for -lPods clang: error: linker command…
angelokh
  • 9,426
  • 9
  • 69
  • 139
347
votes
10 answers

Create singleton using GCD's dispatch_once in Objective-C

If you can target iOS 4.0 or above Using GCD, is it the best way to create singleton in Objective-C (thread safe)? + (instancetype)sharedInstance { static dispatch_once_t once; static id sharedInstance; dispatch_once(&once, ^{ …
Ryan
  • 10,041
  • 27
  • 91
  • 156
346
votes
18 answers

How to disable back swipe gesture in UINavigationController on iOS 7

In iOS 7 Apple added a new default navigation behavior. You can swipe from the left edge of the screen to go back on the navigation stack. But in my app, this behavior conflicts with my custom left menu. So, is it possible to disable this new…
ArtFeel
  • 11,701
  • 4
  • 29
  • 41
346
votes
33 answers

What are best practices that you use when writing Objective-C and Cocoa?

I know about the HIG (which is quite handy!), but what programming practices do you use when writing Objective-C, and more specifically when using Cocoa (or CocoaTouch).
pixel
  • 5,298
  • 8
  • 35
  • 32
345
votes
7 answers

Convert an NSURL to an NSString

I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath. Now in the case of the built-in app images I do get…
Ali
  • 4,205
  • 4
  • 25
  • 40