It is currently Fri Apr 03, 2020 6:07 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: NSString stringWithFormat
PostPosted: Tue Nov 12, 2019 7:05 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 244
Location: NL
Trying to get this working...

Code:
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..


Top
 Profile  
Reply with quote  
 Post subject: Re: NSString stringWithFormat
PostPosted: Tue Nov 12, 2019 7:14 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
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:
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)

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: NSString stringWithFormat
PostPosted: Tue Nov 12, 2019 10:32 am 
Offline
Enthusiast
Enthusiast

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 244
Location: NL
Thanks again, especially for the examples.

ps. IMP?


Top
 Profile  
Reply with quote  
 Post subject: Re: NSString stringWithFormat
PostPosted: Tue Nov 12, 2019 11:24 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
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:
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)

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: NSString stringWithFormat
PostPosted: Tue Nov 12, 2019 8:04 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2334
Location: Germany
Alternative: Format string like sprintf.

Link:https://www.purebasic.fr/english/viewtopic.php?f=12&t=32026

Code:
  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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye