Objective C : Expected Body of Lambda expression - objective-c

I am working on a small xcode project (my first project) using objective C and am trying to display a variable onto my label in the UI. I have declared the label onto the header file by "ctrl + connecting" it and creating a weak outlet connection but when I try to assign the text value of the label to my variable I get an error saying Expected body of lambda expression
Here is the snippet of the code that throws this error. I essentially want to assign the the variable words as the text for my label:
- (void)speak:(NSString *)words {
NSLog(#"%#", words);
labelForCategory.text = [words];
}
Any suggestions on how to navigate through this will be very helpful. The error is shown on the line labelForCategory.text = [words];

Words is a NSString object so remove [] as
- (void)speak:(NSString *)words {
NSLog(#"%#", words);
labelForCategory.text = words;
}

Related

Swift parameters are not visible inside of Objective C Function?

Some background: I'm building an app in react native that uses an Objective C library written about 6 or 7 years ago, maybe older. I'm writing swift code that has been set up to send callbacks to the react native application in JS. I have this function that I'm trying to use:
token = service.getUserToken(server,
port: P2PFunctions.tls_port,
appId: P2PFunctions.appID,
appSecret: P2PFunctions.appSecret,
phone: P2PFunctions.phone,
token: nil, errcode: errCode, errmsg: nil);
callback(["\(token!)"]);
And this is its definition:
- (NSInteger)getUserToken:(NSString*)ip_In port:(NSInteger)port_In appId:
(NSString*)appId_In appSecret:(NSString*)appSecret_In phone:
(NSString*)phoneNum_In token:(NSString**)accessTok_Out errcode:
(NSString**)strErrCode_Out errmsg:(NSString**)errMsg_Out;
These are the types I'm using (EDIT: I changed them from private to public, and they still are not being recognized):
The problem is, I'm getting nil back from the function. I believe I'm getting an HTTP response that is empty, and I notice that inside the debugger when I step to the Objective C function, I see nil for all my parameters inside of the Objective C function. I think... it is that I'm not passing the correct type. Or my Swift parameters are not visible in Objective C's memory space. If it is expecting an (NSString *), should I be passing a String?
How do I pass the correct types from Swift to Objective C? What would I change in my function call? Are my parameter types okay? I cannot edit the original Objective C library. They share a common memory space for all variables in the entire program, right?
Thank you so much!
I just ran this successfully:
// the objc part
#interface Test : NSObject
- (NSInteger)getUserToken:(NSString*)ip_In
port:(NSInteger)port_In
appId:(NSString*)appId_In
appSecret:(NSString*)appSecret_In
phone:(NSString*)phoneNum_In
token:(NSString* _Nonnull * _Nonnull)accessTok_Out
errcode:(NSString* _Nonnull * _Nonnull)strErrCode_Out
errmsg:(NSString* _Nonnull * _Nonnull)errMsg_Out;
#end
#implementation Test
- (NSInteger)getUserToken:(NSString*)ip_In
port:(NSInteger)port_In
appId:(NSString*)appId_In
appSecret:(NSString*)appSecret_In
phone:(NSString*)phoneNum_In
token:(NSString**)accessTok_Out
errcode:(NSString**)strErrCode_Out
errmsg:(NSString**)errMsg_Out {
*accessTok_Out = #"Token";
*strErrCode_Out = #"OK";
*errMsg_Out = #"msg";
return 42;
}
#end
// and the swift part
let t = Test()
var token: NSString = "t"
var errcode: NSString = "c"
var errmsg: NSString = "m"
let result = t.getUserToken("ip", port: 1,
appId: "2", appSecret: "3", phone: "4",
token: &token, errcode: &errcode, errmsg: &errmsg)
print(result)
and it works as expected.
Maybe this gives you a hint as to what's different in your situation.
So, after spending a week or so on this problem, I realize that it was not that the parameters are not actually being passed, but that the debugger is just not displaying the values of those parameters, at least on the main thread. Because I was getting the error code, I thought that something had to be wrong with the way I called the function - but actually, the function call is fine, the variables just didn't appear in the debugger for some reason:
Variables above appear to be nil - but in fact, they do have values.

Evaluate a Cocoa Scripting object from NSAppleEventDescriptor

In my sdef, I have a command that accepts a parameter of type "Any".
In the script, I pass the reference to a scriptable object:
tell application "myApp"
set theArg to first subItem of appElement -- appElement is an element of the app object
myCommand theArg
end
In my command handler code, when fetching the parameter, I get back an object like this:
<NSAppleEventDescriptor: 'obj '{ 'form':'ID ', 'want':'Subi', 'seld':10900, 'from':'obj '{ 'form':'ID ', 'want':'Elem', 'seld':10900, 'from':null() } }>
Now, I like to resolve that back into an NSObject that represents the actual scripting object of "theArg". How do I do that? I can't find any evaluation functions in NSAppleEventDescriptor, other than for simple types such as text, numbers and file references.
Turns out I need again a private function that's been mentioned here as well.
#interface NSScriptObjectSpecifier (NSPrivate)
+ (id) _scriptingSpecifierWithDescriptor:(NSAppleEventDescriptor*) descriptor;
#end
With that, I'll get an NSScriptObjectSpecifier, on which I then invoke objectsByEvaluatingSpecifier to get my object.

Core Data strange issue with properties

I just noticed a strange issue with my app using Core Data (which didn't happen before).
Here is a section of code:
NSArray *allTransactions = [CDMTransaction transactionsFrom:_dateFrom to:_dateTo bankAccounts:_displayedBankAccounts categories:categories recipients:recipients tags:tags unclosedOnly:unclosedTransactionsOnly];
for (CDMTransaction *transaction in allTransactions) {
if (transaction.inverseTransaction != nil) {
...
}
}
What causes the issue is the call to transaction.inverseTransaction.
CDMTransaction is a subclass of NSManagedObject, which contains several properties and relationships (like inverseTransaction).
Everything worked fine until now, but I get an crash when this property is called:
CoreData: error: warning snapshot_get_value_as_object called on NULL, and it sends me to the inverseTransaction property implementation.
But when I try to log the allTransactions array contents, before entering the "for" loop, it just work fine!
Here is the updated code:
NSArray *allTransactions = [CDMTransaction transactionsFrom:_dateFrom to:_dateTo bankAccounts:_displayedBankAccounts categories:categories recipients:recipients tags:tags unclosedOnly:unclosedTransactionsOnly];
NSLog(allTransactions: %#, allTransactions);
for (CDMTransaction *transaction in allTransactions) {
if (transaction.inverseTransaction != nil) {
...
}
}
And what I get in the log console:
2015-01-11 11:54:51.613 Cash[14383:1403274] allTransactions: (
"<CDMTransaction: 0x7b94d4a0> (entity: CDMTransaction; id: 0x7b935f30 <x-coredata://12493FBF-2D83-469C-9610-D7C29E21F12B/CDMTransaction/p1> ; data: <fault>)"
)
Without any issue after...
It doesn't seem to be an issue with the inverseTransaction property because it does the same with all other properties (NSString, XNSNumber, ...) and relationships...
What is wrong?
Edit 1: when it crashes I get the property implementation line highlighted in green with a EXC_BAD_INSTRUCTION error (and not an EXC_BAD_ACCESS)
Edit 2: The app doesn't crash anymore when I add request.returnsObjectsAsFaults = NO; to my NSFetchRequest... So it seems to be OK now, but why do I have to write that? It didn't need it before...

"return" from method while stepping?

I would like to exit out the current method that I'm stepping through.
-(void)helloWorld {
NSLog(#"Hello");
// I would like to return here, so that "World" isn't printed.
NSLog(#"World");
}
I have tried the following, but without luck.
(lldb) expr return
<no result>
Is this possible with lldb?
Unfortunately in Xcode 4.5.x there is no way to force an early return from a function. In the current lldb sources over at http://lldb.llvm.org/ there is a newly added command, thread return, which does what you want - it includes the ability to specify the return value of the function. This won't be in Xcode until the next major release, though.
When you are debugging using Xcode and when your program is paused at a breakpoint, you can drag the little green arrow to any other line in the function. E.g. in the following code:
if I want to skip the NSLog(#"B"), I can simply drag the green arrow from line 20 to line 23, which means the function will simply "return" from anywhere I want.
I just added a breakpoint at the line mentioned below:
var computed: Bool {
return device.time == 10 // added breakpoint here
}
and got the following error:
error: Error returning from frame 0 of thread 1: We only support
setting simple integer and float return types at present..
Seems to work for only those two types

NSFormatter and NSTokenField and setting the maximum length

I would like to use a custom NSFormatter to judge the length of a string inside of a NSTokenField.
I have implemented the NSFormatter and hooked it up within the xib and all that, now when I get to running my application with the formatter inplace connected to the NSTokenField I get this error:
2011-12-31 18:15:11.761 MyApp[4706:a0f] -[NSCFArray length]: unrecognized selector sent to instance 0xf4a530
(gdb) describe 0xf4a530
Undefined command: "describe". Try "help".
(gdb) p 0xf4a530
$1 = 16033072
(gdb) po 0xf4a530
<NSCFArray 0xf4a530>(
{
Format = "%01d";
FormatIndex = 0;
Name = "$Counter$";
},
{
Format = "";
Name = hey;
}
)
Now I understand that indeed the textcell being evaluated contains an array of items so I will need to probably turn these to a string but the place where this is erroring in my code is here:
//Set this so that user can't enter a super long amount and overflow the character array lower in the engine.
[fieldFormatter setMaximumLength:40];
Im just evaluating the fieldFormatter object and setting its member variable of int to 40... really shouldnt be evaluating the text yet... right?