ObjFW  Update of "Differences to OpenStep"

Overview

Artifact ID: 44497a79f7709121237a6b1905a31f7cb6777ce2ce25375a2e12913330f231dc
Page Name:Differences to OpenStep
Date: 2024-06-16 10:23:30
Original User: js
Mimetype:text/x-markdown
Content

While ObjFW is in many ways similar to OpenStep, it is also different in many ways. This page lists some of the differences to make it easier to switch for developers who are already familiar with OpenStep.

Exceptions

One of the main differences between OpenStep and ObjFW is that ObjFW fully embraces exceptions as a first class citizen. Unfortunately, exceptions where not available when OpenStep was designed, so OpenStep is not using them. ObjFW however has been designed when exceptions existed, so there is no reason for it to not make full use of of them. As exceptions are used throughout, this means there is nothing comparable to NSError and no parameter needs to be passed to methods that can fail. Instead, on error, they throw an exception, usually a subclass of OFException. However, exceptions are only used for exceptional cases: Trying to get a key from a dictionary that doesn't exist is not an exception (like in Python for example), but instead just returns nil.

init methods

As a result of ObjFW using exceptions, init methods in ObjFW look different. Neither alloc nor init is allowed to return nil, as on failure, an exception should be thrown instead. This means that the usual init pattern with ObjFW is different.

Without ARC, an init method should look like this:

- (instancetype)init
{
	self = [super init];

	@try {
		_myIVar = somethingThatCanFail();
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

With ARC, -fobjc-arc-exceptions is required, which makes the compiler handle releasing self on an exception automatically. This means that the same init methid with ARC shoud look like this:

- (instancetype)init
{
	self = [super init];

	_myIVar = somethingThatCanFail();
	
	return self;
}