Objective c

From G1Joshi/Agent-Skills · View interactive version

Objective c is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Objective-C assistance covering message passing, runtime dynamic method resolution, ARC, and Foundation/Cocoa. Use when maintaining Apple legacy codebases, developing native iOS/macOS frameworks, or C++ interop.

How to use this skill

Skill file

Objective-C

Objective-C is in maintenance mode. New features are rare, but it powers the Apple ecosystem's foundation. It allows mixing C++ (.mm) loosely.

When to Use

Quick Start

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
- (void)sayHello;
@end

@implementation Person
- (void)sayHello {
    NSLog(@"Hello, my name is %@", self.name);
}
@end

Core Concepts

Dynamic Message Passing (`[receiver message]`)

Method calls in Objective-C are dynamic messages resolved at runtime via objc_msgSend:

#import <Foundation/Foundation.h>

@interface OrderService : NSObject
- (BOOL)processPayment:(double)amount forCustomer:(NSString *)customer;
@end

@implementation OrderService
- (BOOL)processPayment:(double)amount forCustomer:(NSString *)customer {
    NSLog(@"Charging %@: $%.2f", customer, amount);
    return amount > 0.0;
}
@end

// Invocation syntax
OrderService *service = [[OrderService alloc] init];
[service processPayment:150.0 forCustomer:@"Alice"];

Automatic Reference Counting (ARC) & Nullability Annotations

Manages object memory automatically at compile time with strong/weak ownership semantics:

@property (nonatomic, strong) NSString *accountNumber;
@property (nonatomic, weak) id<OrderDelegate> delegate; // Prevents retain cycles
@property (nonatomic, copy) void (^completionHandler)(BOOL success);

Objective-C++ (`.mm`) Bridging

Seamlessly mixes C++ standard library types with Cocoa objects in the same file:

// ProcessorBridge.mm (Objective-C++)
#include <vector>
#import "ProcessorBridge.h"

@implementation ProcessorBridge {
    std::vector<double> _signalBuffer;
}
- (void)addSample:(double)sample {
    _signalBuffer.push_back(sample);
}
@end

Common Patterns

Safe Block Self-Capture to Prevent Retain Cycles

Problem: Strong reference cycles in asynchronous blocks cause permanent memory leaks under ARC.

Solution:

Use the weakSelf / strongSelf dance in completion blocks:

__weak typeof(self) weakSelf = self;
[self.networkClient fetchProfileWithCompletion:^(NSDictionary *data) {
    __strong typeof(weakSelf) strongSelf = weakSelf;
    if (!strongSelf) return;
    [strongSelf updateUIWithData:data];
}];

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :--------------------------------------------- | :--------------------------------------------------------- | :--------------------------------------------------------------------------------------------------- |

| unrecognized selector sent to instance ... | Invoking a method not implemented by the receiving object. | Verify method name and colons (e.g. doAction: vs doAction), or check with respondsToSelector:. |

| EXC_BAD_ACCESS (code=1, address=...) | Dereferencing deallocated memory or zombie object. | Enable Zombie Objects in Xcode scheme diagnostics to locate deallocated reference. |

| Property with 'copy' attribute is not copied | Custom setter failed to call [newValue copy]. | Implement custom setters using _prop = [newValue copy];. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.