0

我设计了一个用于发送基本电子邮件的 UI,可以通过

    SMTPClient  
    deliverMailFrom: sender
to: recipients
text: message
usingServer: '' .

我有一个方法可以接受电子邮件,主题来自用户的所有标题。这是我的方法。

 AddHeader:Headername Email: asString Message:message --> "I want to update this"

 " update is nothing but adding dictionay values to that "
|data|
 data:= Dictionary new.
 'To'= Headername ifTrue[data at:#To put:Email.].
 'From'= Headername ifTrue[data at: #From put:Email].
 'subject'= Headername ifTrue[data at: #subject put:Email].

"adding dicitonay values to message"
message:= String streamContents:[:stream|
data values do:[:each| stream nextPutAll: each ]
    separatedBy: [ stream nextPut: Character space ]].

当我尝试保存此方法时,我收到一条错误消息,提示无法存储到消息中。那么将值添加到已定义的消息中的正确方法是什么。

从字典中添加的其他事物值不会按照放置顺序添加。那么有没有办法我可以添加这些值,以便将它们放在首位,从下一个,然后是主题..

4

1 回答 1

2

我不知道您是否只是在询问错误,或者您是否正在尝试在代码的基础上实现其他目标。由于代码分散且不完整,我无法给你一个正确的答案,但我会改编一个类似于你的代码,它似乎可以工作:

|data|
data:= Dictionary new.
('To'= 'To') ifTrue: [data at:#To put:'someone@mail.com'].
('To'= 'Another thing') ifTrue: [data at:#To put:'shouldnt appear'].
data at:#From put:'me@mail.com'.

message:= String streamContents:
            [:stream|
                    data values 
                            do:[:each| stream nextPutAll: each ]
                            separatedBy: [ stream nextPut: Character space ]].

如果您将其复制到工作区并检查其data内容,它将为您提供以下ByteString 'someone@mail.com me@mail.com'. 但我认为你已经完成了这项任务。您能否详细说明您正在寻找的内容并在您编写代码时放置完整的代码?

根据 OP 说明进行编辑

Ok, now I see the problem. The thing is that in Smalltalk you don't have "out" parameters and that is why the compiler is telling you that it can't store new contents in the message parameter. From the point of view of the object sending the message, you see that message send as telling an object to perform an action and passing the required collaborators to that object to perform it. If there is a response from the message send it is expected to be in the return value. I re-wrote your method a little bit here to do that:

addHeader: headerName email: aString message: aMessage 

| data newValues |

 data:= Dictionary new.
 'To'= headerName ifTrue: [data at:#To put: aString.].
 'From'= headerName ifTrue: [data at: #From put: aString].
 'subject'= headerName ifTrue: [data at: #subject put: aString].

"adding dicitonay values to message"
newValues := String streamContents:[:stream|
data values do:[:each| stream nextPutAll: each ]
    separatedBy: [ stream nextPut: Character space ]].
^aMessage  , newValues.

as you can see the idea is to use the #, message to join the two strings and return the new value. Some things to note here are:

  1. I changed a little bit the names to use a more accepted capitalization pattern.
  2. You may want to take a look at the #asSymbol message to avoid the if statements.

HTH

于 2013-01-28T19:23:02.133 回答