Page 1 of 1
NSString stringWithFormat
Posted: Tue Nov 12, 2019 7:05 am
by Rinzwind
Trying to get this working...
Code: Select all
v.s = " 1234"
t = CocoaMessage(0, 0, "NSString stringWithFormat:$", @"Test %S", ":$", @v)
If t
a.s = PeekS(CocoaMessage(0, t, "UTF8String"), -1, #PB_UTF8)
EndIf
Debug a
Problems with variable argument list..
Re: NSString stringWithFormat
Posted: Tue Nov 12, 2019 7:14 am
by wilbert
Rinzwind wrote:Problems with variable argument list..
Yes, CocoaMessage relies on NSInvocation which does not support variadic methods.
You would have to use a CFString method using ImportC or call NSString stringWithFormat directly using the IMP.
Here's an example using CFString
Code: Select all
ImportC ""
CFStringCreateWithCString(alloc, cStr.p-utf8, encoding = #kCFStringEncodingUTF8)
CFStringCreateWithFormat1(alloc, formatOptions, format, arg1) As "_CFStringCreateWithFormat"
CFStringCreateWithFormat2(alloc, formatOptions, format, arg1, arg2) As "_CFStringCreateWithFormat"
CFStringGetCharacters(theString, location, length, *buffer)
CFRelease(cf)
EndImport
Procedure.s CFStringGetString(theString)
Protected Length.i = CFStringGetLength_(theString)
Protected Dim Buffer.u(Length)
CFStringGetCharacters(theString, 0, Length, @Buffer())
ProcedureReturn PeekS(@Buffer())
EndProcedure
v.s = "1234"
Format = CFStringCreateWithCString(0, "Test %S")
FormattedString = CFStringCreateWithFormat1(0, 0, Format, @v)
Debug CFStringGetString(FormattedString)
CFRelease(FormattedString)
CFRelease(Format)
Format = CFStringCreateWithCString(0, "Test %S [%i]")
FormattedString = CFStringCreateWithFormat2(0, 0, Format, @v, 1234)
Debug CFStringGetString(FormattedString)
CFRelease(FormattedString)
CFRelease(Format)
Re: NSString stringWithFormat
Posted: Tue Nov 12, 2019 10:32 am
by Rinzwind
Thanks again, especially for the examples.
ps. IMP?
Re: NSString stringWithFormat
Posted: Tue Nov 12, 2019 11:24 am
by wilbert
Rinzwind wrote:IMP?
IMP is the address of the implementation itself.
But after some trying, I couldn't get it to work.
Invoking a method however does seem to work and it might also be possible by using NSInvocation in another way.
Maybe I'll try that also.
Here's the invoking way ...
Code: Select all
ImportC ""
method_invoke1(receiver, m, arg1, arg2) As "_method_invoke"
method_invoke2(receiver, m, arg1, arg2, arg3.d) As "_method_invoke"
EndImport
NSStringClass = objc_getClass_("NSString")
Selector = sel_registerName_("stringWithFormat:")
Method = class_getClassMethod_(NSStringClass, Selector)
v.s = "1234"
Format = CocoaMessage(0, 0, "NSString stringWithString:$", @"Test %S")
FormattedString = method_invoke1(NSStringClass, Method, Format, @v)
Debug PeekS(CocoaMessage(0, FormattedString, "UTF8String"), -1, #PB_UTF8)
Format = CocoaMessage(0, 0, "NSString stringWithString:$", @"Test %S [%f]")
FormattedString = method_invoke2(NSStringClass, Method, Format, @v, 12.34)
Debug PeekS(CocoaMessage(0, FormattedString, "UTF8String"), -1, #PB_UTF8)
Re: NSString stringWithFormat
Posted: Tue Nov 12, 2019 8:04 pm
by mk-soft
Alternative: Format string like sprintf.
Link:
viewtopic.php?f=12&t=32026
Code: Select all
name.s = "Apple"
weight.f = 100.5
SQL.s = Format("INSERT INTO food (name, weight) VALUES ('%s', %.3f)", @name, @weight)
Debug SQL