Repository: realm/realm-swift
File: src/tightdb/objc/TDBTransaction.mm (Objective-C++)

Code before:

    TDBTable* table = [[TDBTable alloc] _initRaw];
    if (TIGHTDB_UNLIKELY(!table))
        return nil;
    TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(
        tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name));
        [table setNativeTable:table_2.get()];
    )
    [table setParent:self];
    [table setReadOnly:m_read_only];
    return table;
}

// FIXME: Check that the specified class derives from Table.
-(id)createTableWithName:(NSString*)name asTableClass:(__unsafe_unretained Class)class_obj
{
    if ([name length] == 0) {
       @throw [NSException exceptionWithName:@"tightdb:table_name_exception"
                                      reason:@"Name must be a non-empty NSString"
                                    userInfo:nil];
    }

    if (m_read_only) {
        @throw [NSException exceptionWithName:@"tightdb:core_read_only_exception"
                                       reason:@"Transaction is read-only."
                                     userInfo:nil];
    }

    if ([self hasTableWithName:name]) {
        @throw [NSException exceptionWithName:@"tightdb:table_with_name_already_exists"
                                       reason:[NSString stringWithFormat:@"A table with the name '%@' already exists in the context.", name]
                                     userInfo:nil];
    }

    TDBTable* table = [[class_obj alloc] _initRaw];
    if (TIGHTDB_UNLIKELY(!table))
        return nil;
    bool was_created;
    TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(
        tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name), was_created);
        [table setNativeTable:table_2.get()];)
    [table setParent:self];
    [table setReadOnly:m_read_only];
    if (was_created) {
        if (![table _addColumns])
            return nil;
    }
    else {
        if (![table _checkType])
            return nil;
    }

Review comment (suggestion):
You can pass `nil` as `userInfo` above.

You should throw the exception the same we we do here https://github.com/Tightdb/tightdb_objc/blob/master/src/tightdb/objc/TDBTable.mm#L77.


Code after:

    TDBTable* table = [[TDBTable alloc] _initRaw];
    if (TIGHTDB_UNLIKELY(!table))
        return nil;
    TIGHTDB_EXCEPTION_HANDLER_CORE_EXCEPTION(
        tightdb::TableRef table_2 = m_group->get_table(ObjcStringAccessor(name));
        [table setNativeTable:table_2.get()];
    )
    [table setParent:self];
    [table setReadOnly:m_read_only];
    return table;
}

-(TDBTable *)createTableWithName:(NSString*)name columns:(NSArray*)columns
{
    TDBTable* table = [self createTableWithName:name];
    
    //Set columns
    tightdb::TableRef nativeTable = [table getNativeTable].get_table_ref();
    if (!set_columns(nativeTable, columns)) {
        // Parsing the schema failed
        //TODO: More detailed error msg in exception
        NSException* exception = [NSException exceptionWithName:@"tightdb:invalid_columns"
                                                         reason:@"The supplied list of columns was invalid"
                                                       userInfo:[NSMutableDictionary dictionary]];
        [exception raise];
    }
    
    return table;
}

// FIXME: Check that the specified class derives from Table.
-(id)createTableWithName:(NSString*)name asTableClass:(__unsafe_unretained Class)class_obj
{
    if ([name length] == 0) {
       @throw [NSException exceptionWithName:@"tightdb:table_name_exception"
                                      reason:@"Name must be a non-empty NSString"
                                    userInfo:nil];
    }

    if (m_read_only) {
        @throw [NSException exceptionWithName:@"tightdb:core_read_only_exception"
                                       reason:@"Transaction is read-only."
                                     userInfo:nil];
    }

    if ([self hasTableWithName:name]) {
        @throw [NSException exceptionWithName:@"tightdb:table_with_name_already_exists"
                                       reason:[NSString stringWithFormat:@"A table with the name '%@' already exists in the context.", name]
                                     userInfo:nil];
    }