diff --git a/cpp/src/slice2objc/Gen.cpp b/cpp/src/slice2objc/Gen.cpp index c52ea9bbf17..3923e3fabaf 100644 --- a/cpp/src/slice2objc/Gen.cpp +++ b/cpp/src/slice2objc/Gen.cpp @@ -3106,23 +3106,14 @@ Slice::Gen::DelegateMVisitor::visitOperation(const OperationPtr& p) if(returnType) { _M << nl << outTypeToString(returnType, p->returnIsOptional(), true) << " ret"; - if(!isValueType(returnType)) - { - _M << " = nil;"; - } - else - { - _M << ";"; - } + _M << " = " << defaultValue(returnType, p->returnIsOptional()) << ";"; } if(p->returnsData()) { for(ParamDeclList::const_iterator op = outParams.begin(); op != outParams.end(); ++op) { - if(!isValueType((*op)->type())) - { - _M << nl << "*" << getParamName(*op, true) << " = nil;"; - } + _M << nl << "*" << getParamName(*op, true) << " = " << defaultValue((*op)->type(), (*op)->optional()) + << ";"; } } _M << nl << "if(!ok)"; diff --git a/cpp/src/slice2objc/ObjCUtil.cpp b/cpp/src/slice2objc/ObjCUtil.cpp index 1848cbcbf4d..cf7e34fd5f0 100644 --- a/cpp/src/slice2objc/ObjCUtil.cpp +++ b/cpp/src/slice2objc/ObjCUtil.cpp @@ -427,6 +427,46 @@ Slice::ObjCGenerator::typeToObjCTypeString(const TypePtr& type) } } +string +Slice::ObjCGenerator::defaultValue(const TypePtr& type, bool isOptional) +{ + if(isValueType(type) && !isOptional) + { + if(EnumPtr::dynamicCast(type)) + { + return "0"; + } + BuiltinPtr builtin = BuiltinPtr::dynamicCast(type); + if(builtin) + { + switch(builtin->kind()) + { + case Builtin::KindBool: + return "NO"; + case Builtin::KindByte: + case Builtin::KindShort: + case Builtin::KindInt: + case Builtin::KindLong: + return "0"; + case Builtin::KindFloat: + case Builtin::KindDouble: + return "0.0"; + default: + { + assert(false); + return "???"; + } + } + } + assert(false); + return "???"; + } + else + { + return "nil"; + } +} + bool Slice::ObjCGenerator::isValueType(const TypePtr& type) { diff --git a/cpp/src/slice2objc/ObjCUtil.h b/cpp/src/slice2objc/ObjCUtil.h index 0c5176d67d8..4520ee7082a 100644 --- a/cpp/src/slice2objc/ObjCUtil.h +++ b/cpp/src/slice2objc/ObjCUtil.h @@ -52,6 +52,7 @@ class ObjCGenerator : private ::IceUtil::noncopyable static std::string inTypeToString(const TypePtr&, bool, bool = false, bool = false); static std::string outTypeToString(const TypePtr&, bool, bool = false, bool = false); static std::string typeToObjCTypeString(const TypePtr&); + static std::string defaultValue(const TypePtr&, bool); static bool isValueType(const TypePtr&); static bool isString(const TypePtr&); static bool isClass(const TypePtr&); diff --git a/objective-c/test/Ice/defaultValue/Client.m b/objective-c/test/Ice/defaultValue/Client.m index bc36e6977ee..9983d7030c0 100644 --- a/objective-c/test/Ice/defaultValue/Client.m +++ b/objective-c/test/Ice/defaultValue/Client.m @@ -7,7 +7,7 @@ #import static int -run() +run(void) { void defaultValueAllTests(void); defaultValueAllTests(); diff --git a/objective-c/test/Ice/dispatcher/AllTests.m b/objective-c/test/Ice/dispatcher/AllTests.m index f237c0492c8..a46e22f6414 100644 --- a/objective-c/test/Ice/dispatcher/AllTests.m +++ b/objective-c/test/Ice/dispatcher/AllTests.m @@ -8,7 +8,7 @@ #import -static BOOL isDispatcherThread() +static BOOL isDispatcherThread(void) { return strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), "Dispatcher") == 0; } diff --git a/objective-c/test/Ice/hash/Client.m b/objective-c/test/Ice/hash/Client.m index 867593665b6..e942196b4f3 100644 --- a/objective-c/test/Ice/hash/Client.m +++ b/objective-c/test/Ice/hash/Client.m @@ -7,7 +7,7 @@ #import static int -run() +run(void) { void hashAllTests(void); hashAllTests(); diff --git a/objective-c/test/Ice/services/AllTests.m b/objective-c/test/Ice/services/AllTests.m index 44102d01730..11d47dab75a 100644 --- a/objective-c/test/Ice/services/AllTests.m +++ b/objective-c/test/Ice/services/AllTests.m @@ -50,8 +50,8 @@ -(void)tick:(NSString*)time current:(ICECurrent*)__unused current ICESTORMTopicManagerPrx* manager = [ICESTORMTopicManagerPrx uncheckedCast:[communicator stringToProxy:@"test:default -p 12010"]]; - ICESTORMQoS* qos; - ICESTORMTopicPrx* topic; + ICESTORMQoS* qos = nil; + ICESTORMTopicPrx* topic = nil; NSString* topicName = @"time"; id adapter = [communicator createObjectAdapterWithEndpoints:@"subscriber" endpoints:@"tcp"]; diff --git a/objective-c/test/Slice/escape/Client.m b/objective-c/test/Slice/escape/Client.m index bc18968677e..805c417dfd6 100644 --- a/objective-c/test/Slice/escape/Client.m +++ b/objective-c/test/Slice/escape/Client.m @@ -29,7 +29,7 @@ @interface andfriendI : ICELocalObject // are named correctly. It is not expected to run. // static void -testSymbols() +testSymbols(void) { andbreakPrx* prx1 = 0; [prx1 case_:0 try:0];