# Suggestions for wrapping an Objective C or C++ class hierarchy?

**URL:** <https://discourse.julialang.org/t/suggestions-for-wrapping-an-objective-c-or-c-class-hierarchy/73050>\
**Category:** General Usage\
**Created:** [December 13, 2021, 8:53pm UTC](https://discourse.julialang.org/t/suggestions-for-wrapping-an-objective-c-or-c-class-hierarchy/73050 "2021-12-13T20:53:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertN](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@RobertN](https://discourse.julialang.org/u/RobertN)\
**Post date:** [December 13, 2021, 8:53pm UTC](https://discourse.julialang.org/t/suggestions-for-wrapping-an-objective-c-or-c-class-hierarchy/73050/1 "2021-12-13T20:53:14Z")

</div>

I’m looking to wrap Apple’s Objective-C class hierarchy in AppKit. I’m reading [the docs on calling C code](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#mapping-c-types-to-julia), but it’s not obvious yet how to best wrap a class hierarchy. Any suggestions?

Example type relations, from the [Objective C library](https://developer.apple.com/documentation/appkit/nstextfield?language=objc):

```nohighlight
NSTextField <: NSControl <: NSView <: NSResponder <: NSObject
NSTableView <: NSControl ...
NSWindow <: NSResponder ... 

```

Would I declare empty mutable structs in Julia, and use `ccall` with `Ptr` to them?

```julia
mutable struct NSWindow end
mutable struct NSView end
mutable struct NSTextField end

ccall(:addSubview, Nothing, (Ptr{NSView}, Ptr{NSView}), cv, tf)

```

In Objective C (technically I’m using Objective C++):

```julia
extern "C" void addSubview(NSView *a, NSView *b) {
    [a addSubview:b];
}

```

That actually “works” in my little test program I’ve got so far, but as you can see I’m not declaring any kind of relationship between the types.

Maybe I can call [objc\_msgSend](https://developer.apple.com/documentation/objectivec/1456712-objc_msgsend/) with `ccall`, and avoid writing lots of little wrapper functions like `addSubview` there.

---

<div class="post-metadata">

**Author:** ![Erik\_Engheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erik_engheim/32/8122_2.png) [@Erik\_Engheim](https://discourse.julialang.org/u/Erik_Engheim)\
**Post date:** [February 13, 2022, 3:22am UTC](https://discourse.julialang.org/t/suggestions-for-wrapping-an-objective-c-or-c-class-hierarchy/73050/2 "2022-02-13T03:22:10Z")

</div>

The objc\_msgSend approach is what Mike Innes is doing in this library:

> **[GitHub - JuliaInterop/ObjectiveC.jl: Objective-C embedded in Julia](https://github.com/JuliaInterop/ObjectiveC.jl)**
>
> Objective-C embedded in Julia. Contribute to JuliaInterop/ObjectiveC.jl development by creating an account on GitHub.

Unfortunately this is outdated, but I have been spending some time now to try to get a version working for Julia 1.x. I have just been hacking on it in this weekend and it seems work quite well. It can give you a lot of cool functionality such as being able to define your own Objective-C classes in Julia with nice syntax.
