NSString stringWithFormat

Mac OSX specific forum
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

NSString stringWithFormat

Post 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..
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: NSString stringWithFormat

Post 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)
Windows (x64)
Raspberry Pi OS (Arm64)
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: NSString stringWithFormat

Post by Rinzwind »

Thanks again, especially for the examples.

ps. IMP?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: NSString stringWithFormat

Post 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)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5388
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: NSString stringWithFormat

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply