ObjFW  Artifact [44497a79f7]

Artifact 44497a79f7709121237a6b1905a31f7cb6777ce2ce25375a2e12913330f231dc:

Wiki page [Differences to OpenStep] by js on 2024-06-16 10:23:30.
D 2024-06-16T10:23:30.388
L Differences\sto\sOpenStep
N text/x-markdown
U js
W 1842
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:

```objc
- (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:

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

	_myIVar = somethingThatCanFail();
	
	return self;
}
```
Z 84ec424548673ea94ea9134e35ecdce4